add alive check for gateway and fix webhook message event bug

This commit is contained in:
hippoz 2022-02-06 22:58:38 +02:00
parent e33f6f7cfd
commit aae5ce66ca
No known key found for this signature in database
GPG key ID: 7C52899193467641
4 changed files with 33 additions and 10 deletions

View file

@ -18,6 +18,8 @@ class GatewayServer {
...extraWebsocketServerConfig ...extraWebsocketServerConfig
}); });
setInterval(this.onAliveCheck.bind(this), 30000);
this.wss.on("connection", (ws) => { this.wss.on("connection", (ws) => {
this.onConnection(ws); this.onConnection(ws);
ws.on("message", (data, isBinary) => { ws.on("message", (data, isBinary) => {
@ -25,7 +27,10 @@ class GatewayServer {
}); });
ws.on("close", (code, reason) => { ws.on("close", (code, reason) => {
this.onDisconnect(code, reason.toString()); this.onDisconnect(code, reason.toString());
}) });
ws.on("pong", () => {
this.onPong(ws);
});
}); });
} }
@ -187,6 +192,20 @@ class GatewayServer {
} }
} }
} }
onPong(ws) {
this._clientDispatch(ws, { type: "SET_ALIVE", alive: true });
}
onAliveCheck() {
this.wss.clients.forEach(ws => {
if (ws.isAlive === false)
return ws.terminate();
this._clientDispatch(ws, { type: "SET_ALIVE", alive: false });
ws.ping();
});
}
} }
export default GatewayServer; export default GatewayServer;

View file

@ -59,11 +59,6 @@ class WatchedGuild extends EventEmitter {
if (message.guild_id !== this.upstreamGuildId) if (message.guild_id !== this.upstreamGuildId)
return; return;
// TODOOOOOO: bridge user's wont get message events from other bridge users in the same channel
const maybeKnownWebhook = this.knownWebhooks.get(message.channel_id);
if (maybeKnownWebhook && maybeKnownWebhook.id === message.webhook_id)
return; // ignore messages coming from our webhook
this.pushEvent({ this.pushEvent({
eventType: "MESSAGE_CREATE", eventType: "MESSAGE_CREATE",
message: message message: message

View file

@ -15,6 +15,11 @@
function handleEvent(event) { function handleEvent(event) {
if (event.eventType === "MESSAGE_CREATE") { if (event.eventType === "MESSAGE_CREATE") {
// ignore messages that are seemingly coming from ourselves
if (event.message.webhook_id && event.message.author.username === user.username) {
return;
}
const guildId = event.message.guild_id; const guildId = event.message.guild_id;
const channelId = event.message.channel_id; const channelId = event.message.channel_id;
@ -69,13 +74,13 @@
selectedChannel = guilds[0].channels[0]; selectedChannel = guilds[0].channels[0];
} }
if (supportsWebsockets()) { if (supportsWebsockets() && !localStorage.getItem("f_disableWebsockets")) {
console.log("App: browser supports WebSocket, using gateway"); console.log("App: browser supports WebSocket, using gateway");
const gatewayConnection = new GatewayClient(`${ location.protocol === "https:" ? "wss://" : "ws://" }${location.host}/gateway`); const gatewayConnection = new GatewayClient(`${ location.protocol === "https:" ? "wss://" : "ws://" }${location.host}/gateway`);
gatewayConnection.onEvent = handleEvent; gatewayConnection.onEvent = handleEvent;
gatewayConnection.connect(apiClient.token); gatewayConnection.connect(apiClient.token);
} else { } else {
console.warn("App: browser does not WebSocket, using polling"); console.warn("App: browser does not support WebSocket, using polling");
const { poll } = apiClient.createPollingListener(null, ({ event }) => { const { poll } = apiClient.createPollingListener(null, ({ event }) => {
handleEvent(event); handleEvent(event);
}); });

View file

@ -43,8 +43,12 @@
<div class="card full-card option-card"> <div class="card full-card option-card">
<span class="main-panel-header">{ title }</span> <span class="main-panel-header">{ title }</span>
{#each displayedElements as element} {#each displayedElements as element, index}
{#if index === 0}
<button class="button button-selected option-button" on:click={elementClicked(element.id)}>{ element.name }</button>
{:else}
<button class="button option-button" on:click={elementClicked(element.id)}>{ element.name }</button> <button class="button option-button" on:click={elementClicked(element.id)}>{ element.name }</button>
{/if}
{/each} {/each}
{#if displayedElements.length < 1} {#if displayedElements.length < 1}
<div class="center-text"> <div class="center-text">