I've been trying to test out WebSockets with a secure server and it doesnt work so great.
Is there something I'm missing or is WSS broken currently in v0.4.0?
import { serve, } from "bun";
serve({
port: 3000,
keyFile: "./localhost.key",
certFile: "./localhost.pem",
websocket: {
message(ws, msg) {
ws.send('sup dawg?');
},
},
fetch(req, server) {
if (
req.url.endsWith('/chat') &&
server.upgrade(req)
) {
return new Response("", {
status: 101,
});
}
console.log(
"fetch",
req,
);
return new Response(
`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebSockets</title>
</head>
<body>
<style></style>
<script>
const ws = new WebSocket("wss://localhost:3000/chat");
ws.onopen = (e) => {
ws.send("ping");
};
ws.onmessage = (e) => {
console.log('ws msg', e)
};
</script>
<div id="app">
<h1>bun --hot ./index.js</h1>
</div>
</body>
</html>
`,
{
headers: {
"Content-Type": "text/html; charset=utf-8",
},
},
);
},
});
Tried that with bun run index.js and bun --hot index.js. It goes boom with both.
[1] 183168 segmentation fault (core dumped) bun run index.js
WebSocket connection to 'wss://localhost:3000/chat' failed:
I've tried various combinations and it seems to work with the keyFile & certFile disabled but not with them enabled.
the localhost certs were generated with https://github.com/FiloSottile/mkcert
Work fine with just HTTPS so far as I can tell, just not when websockets are added in.