-
Notifications
You must be signed in to change notification settings - Fork 14
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
koa responding with a 404, with all other middlewares turned off, even with respond = false #6
Comments
oh, one must use the socket during the http upgrade req/res connection for the ws connection to establish itself. hmm. so it's a socket that can only exist during the lifecycle of the request/response? is that correct? if so, why use a socket at all then? alt, is this line supposed to be one block out, one line down? Line 38 in 73eae92
|
I don't really know how you got this issue without seeing how you tried to use The You must retrieve the socket while the upgrade request is still alive, that's just the way HTTP works. It doesn't matter if you do it by calling You could probably add Could you post some example code to show how you're encountering this issue? |
right. everything you said makes sense, but i suppose i'm questioning the value behind it. it just doesn't seem that useful that |
Well, if you'd like to use the socket outside of your request handler function, you'll need to move it somewhere. I tend to just do something like this: const handler = async (ctx) => {
if (ctx.ws) {
const ws = await ctx.ws()
ws.on('message', (message) => {
// react to incoming messages here
})
// send any initial messages if needed
ws.send('hello there')
// also, don't handle the request in any other way, ctx.ws() already does it
return
}
ctx.body = 'this is a websocket route'
ctx.response.status = 400
} But if you prefer to have your event handler somewhere else, you can just move the retrieved I suppose this is the kind of cache you would like to use there. Honestly, I don't think this library will ever help you with that, because that would require making assumptions about your code structure. If this is a frequent use case for you, you could consider writing a companion module to use with this one. Maybe something like this? const handler = async (ctx) => {
if (ctx.ws) {
const ws = await ctx.ws()
yourCache.addSocket(ws)
// no need to handle the request in any other way
return
}
ctx.body = 'this is a websocket route'
ctx.response.status = 400
} As for why |
problem
new WebSocket('wss://localhost:7777')
from a browseryields
when using only this middleware
discussion
i see that
respond = false
, but then again: https://github.com/koajs/koa/blob/master/docs/api/context.md#ctxrespondThe text was updated successfully, but these errors were encountered: