You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constserver=Bun.serve({fetch(req){console.log('SERVER incoming request');server.upgrade(req);},websocket: {open(ws){console.log(`SERVER open`);ws.ping();// removing this fixes the issue - this is probably the buggy part},close(ws,code,reason){console.log(`SERVER close: code=${code} reason=${reason}`);},message(ws,message){console.log(`SERVER message: ${Bun.inspect(message)}`);}},port: 1337,});constws=newWebSocket('ws://127.0.0.1:1337');ws.onopen=async()=>{// same behavior with addEventHandlerws.send('this message sends just fine');forawait(const_ofconsole){// removing this fixes the issuews.send('hi');// causes closure upon first line}};
output with code shown above
% bun run index.js
SERVER incoming request
SERVER open
SERVER message: "this message sends just fine"
output with server ws.ping()or client for await (...) excluded
% bun run index.js
SERVER incoming request
SERVER open
SERVER message: "this message sends just fine"
<-- ENTER (for await over console)
SERVER close: code=1006 reason=
I haven't a clue what's actually causing this, but the presence or absence of a ping doesn't seem like it should cause any problems. I previously had handlers for ping/pong messages, I was just testing them out (no intervals yet), but the issue was there before as well - I tried to reduce the fluff as much as possible to get a simple reproducible
Encountering this issue as well. The bun websocket client seems to have a bug in regards to receiving pings that causes the connection to close?
I also have a simple ServerWebSocket setup and a WebSocket like you, and am able to get some intervaled pings to work, but not others.
ping("42") for example works, but ping() or ping("hello") does not.
Very strange.
// Serverlettimer;Bun.serve({fetch(req,server){// upgrade the request to a WebSocketif(server.upgrade(req)){return;// do not return a Response}returnnewResponse("Upgrade failed :(",{status: 500});},websocket: {open(ws){timer=setInterval(()=>{console.log("server tx'd ping. ");ws.ping("test");},2000);},message(ws,message){lethexString=message.toString("hex");lethexWithSpaces=hexString.match(/.{1,2}/g)!.join(" ");console.log("server rx'd:",hexWithSpaces);},close(ws,code,message){console.log("server closed:",code,message);clearInterval(timer!);},},});// Clientconstsocket=newWebSocket("ws://localhost:3000");socket.addEventListener("message",(event)=>{console.log("client rx'd:",event.data);});socket.addEventListener("error",(event)=>{console.error("client error:",event);});socket.addEventListener("close",(event)=>{console.log("client close:",event.code,event.reason);});
ws.ping("test") fails:
server tx'd ping.
server closed: 1006
client close: 1006 Connection ended
but ws.ping("42") works:
server tx'd ping.
server tx'd ping.
server rx'd: 34 32 b6 67 88 c1 5c 13 bc f3
server tx'd ping.
server tx'd ping.
server rx'd: 34 32 4b f3 7a 4d bf ba 4e 7f
minimal reproducible
output with code shown above
output with server
ws.ping()
or clientfor await (...)
excludedI haven't a clue what's actually causing this, but the presence or absence of a ping doesn't seem like it should cause any problems. I previously had handlers for ping/pong messages, I was just testing them out (no intervals yet), but the issue was there before as well - I tried to reduce the fluff as much as possible to get a simple reproducible
Originally reported on Discord:
websocket - potential bug, weird behavior with ping/pong, maybe I'm missing something
The text was updated successfully, but these errors were encountered: