-
-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot clone a response to a WebSocket handshake #3250
Comments
Hi @JustJoostNL This script works well: import { Hono } from 'hono'
import { upgradeWebSocket } from 'hono/cloudflare-workers'
import type { WSContext } from 'hono/ws'
const app = new Hono()
app.get('/', (c) => {
return c.html(
<html>
<head>
<meta charset='UTF-8' />
</head>
<body>
<div id='time'></div>
<script
dangerouslySetInnerHTML={{
__html: `
const ws = new WebSocket('ws:https://localhost:8787/ws')
const $time = document.getElementById('time')
ws.onmessage = (event) => {
$time.textContent = event.data
}
ws.onopen = (event) => {
ws.send('time')
}
`,
}}
></script>
</body>
</html>
)
})
app.get(
'/ws',
upgradeWebSocket(() => {
return {
onClose: (_ev, ws) => {
ws.send('Goodbye!')
},
onError: (_ev, ws) => {
ws.send('An error occurred')
},
onMessage: socketRouter,
}
})
)
function socketRouter(event: any, ws: WSContext) {
switch (event.data) {
case 'ping':
ws.send('pong')
break
case 'time':
handleTimeRequest(ws)
break
default:
ws.send('Unknown command')
break
}
}
function handleTimeRequest(ws: WSContext) {
setInterval(() => {
ws.send(new Date().toISOString())
}, 1000)
}
export default app |
Thanks! I'll try that. Is there a reason that HTML is required? I think remember it working before without. |
Where do you run a WebSockets client? In my case, I wrote the HTML and created the client on it. |
I've just tried your suggestion, but unfortunately, I'm still experiencing the exact same error as before. |
What version of Hono are you using?
4.5.4
What runtime/platform is your app running on?
Cloudflare Workers
What steps can reproduce the bug?
What is the expected behavior?
The websocket connects like normal.
What do you see instead?
When trying to connect to the websocket:
Additional information
No response
The text was updated successfully, but these errors were encountered: