Description
I've tried to implement some simple scenario when the server sends some text to the client immediately after the connection.
The (simplified) server looks like:
var ws = require("nodejs-websocket");
var wserver = ws.createServer(function (conn) {
console.log("New connection");
setTimeout(function(){
conn.sendText(JSON.stringify({
type:'hello'
}));
},0);
conn.on("close", function (code, reason) {
console.log("Connection closed");
})
})
wserver.listen(8001);
The (simplified) client looks like:
var ws = require("nodejs-websocket");
var conn = ws.connect("ws:127.0.0.1:8001",function() {
console.log("New connection");
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
conn.on("error", function (err) {
console.log("Connection ERROR!",err)
})
});
When the first client connects to the server it looks fine, the client prints "New connection"
But when the second (and all following) client connects to the server, the client doesn't recognise a connection readyness (i.e. doesn't print "New connection" message).
The server recognises and reports the both connections.
The network traffic grabbed by the wireshark looks the same (except security cookies).
The only difference is a time between last server handshake packet and the text packet sent from the server after the handshake.
To check the problem, I've tried two cases:
- I've removed immediate text send from the server code. The bug disappeared.
- I've changed timeout from 0 to 100 to delay a text send. The bug disappeared.
From my point of view it might be caused by some inconsistency in the client code, when the client doesn't separate a server handshake packet from the next packet if it follows immediately to the last websockets handshake packet and concatenated by the TCP layer to one piece of data sent to the user layer.