Micha
Micha
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
Nice thanks
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
Ah I see. In this case, in don't know how that would work.
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
No description
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
That's exactly what I need. Thank you very much!
25 replies
HHono
Created by Ketty on 6/12/2024 in #help
get with params not working
7 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
Will the local _ws object be garbage collected, when it's removed from the webSocketClients array?
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
import { Hono } from 'hono';
import { createBunWebSocket } from 'hono/bun';
import { WSContext } from 'hono/dist/types/helper/websocket';

const { upgradeWebSocket, websocket } = createBunWebSocket();

const app = new Hono();
const webSocketClients: WSContext[] = [];

app.get('/', upgradeWebSocket(() => {
let _ws: WSContext;

return {
onOpen(_, ws: WSContext) {
_ws = ws;
webSocketClients.push(ws);
},
onMessage(event) {
webSocketClients.forEach(webSocketClient => { // broadcast
if (webSocketClient === _ws) { return; } // possible to check, if current `webSocketClient` is sender
webSocketClient.send(`${event.data}`);
});
},
onClose() {
const disconnectedWebSocketClientIndex = webSocketClients.indexOf(_ws);
if (disconnectedWebSocketClientIndex === -1) { return; }

webSocketClients.splice(disconnectedWebSocketClientIndex, 1);
}
};
}));

Bun.serve({
fetch: app.fetch,
websocket
});
import { Hono } from 'hono';
import { createBunWebSocket } from 'hono/bun';
import { WSContext } from 'hono/dist/types/helper/websocket';

const { upgradeWebSocket, websocket } = createBunWebSocket();

const app = new Hono();
const webSocketClients: WSContext[] = [];

app.get('/', upgradeWebSocket(() => {
let _ws: WSContext;

return {
onOpen(_, ws: WSContext) {
_ws = ws;
webSocketClients.push(ws);
},
onMessage(event) {
webSocketClients.forEach(webSocketClient => { // broadcast
if (webSocketClient === _ws) { return; } // possible to check, if current `webSocketClient` is sender
webSocketClient.send(`${event.data}`);
});
},
onClose() {
const disconnectedWebSocketClientIndex = webSocketClients.indexOf(_ws);
if (disconnectedWebSocketClientIndex === -1) { return; }

webSocketClients.splice(disconnectedWebSocketClientIndex, 1);
}
};
}));

Bun.serve({
fetch: app.fetch,
websocket
});
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
No description
25 replies
HHono
Created by blurSkye πŸ‡΅πŸ‡ΈπŸ‰ on 6/7/2024 in #help
websocket issue
@blurSkye πŸ‡΅πŸ‡ΈπŸ‰ I guess, I found a workaround. I tried to store the ws object in a local variable _ws to create a closure like behavior. _ws can then be used later on in every other event like onMessage or onClose. It doesn't seem like a general good solution, because we just omit the provided ws object in the other events. But as a workaround, it works πŸ˜„
25 replies