开发者

Send WebSocket message immediately after handshake

开发者 https://www.devze.com 2023-03-17 07:21 出处:网络
I\'m building a WebSocket server in node.js. When a client has connected, I\'d like to send a message immediately to it.

I'm building a WebSocket server in node.js. When a client has connected, I'd like to send a message immediately to it.

However, when I send it just after the handshake has completed, I get an error in Chrome:

Status line contains embedded null

When I use a setTimeout to send the message 100 milliseconds after the handshake, it works, but then again it does not work when using 10 milliseconds using this technique.

It looks like the handshake and the message get appended when sent together in case the time interval between them is small (though I'm not sure since I don't know how to view raw WebSocket traffic in Chrome - if someone does I'd love to hear that as well).

I do not want to have the server wait 100 milliseconds because it is certainly noticeable for the client, and moreover it looks like a dirty trick rat开发者_如何学Goher than a nice solution.

How can I send a message immediately after a handshake?


I sorted it out already - I was sending it when there was a connection, but this is before the handshake is sent of course. So I was doing:

client connected
send message
receive handshake request
send handshake response
...

I just altered my code by firing the connect event of my library when the handshake is sent, which is when the WebSocket connection is really established.

As a side note, I would like to mention that chrome://net-internals/ is a very helpful page when debugging WebSocket connections (you can easily see the request/response handshake). I should have known that earlier...

0

精彩评论

暂无评论...
验证码 换一张
取 消