Socket.io raw data will disconnect the client

I am working in a chat room, and I noticed that sometimes the connection between my node.js server and iOS will be disconnected right after the server issues some data.

I continuously reported two events based on the logs on the client, it seems that the emitted data is β€œmerged”:

doQueue() >> 0 2013-03-16 05:11:45.390 [833:907] start/reset timeout 2013-03-16 05:11:45.491 [833:907] onData  187 5:::{"name":"threadInformation","args":[{"threadObjects":[{"threadId":"heacrsi1","users":[{"userName":"tester","userId":"123"},{"userName":"Name","userId":"123"}]}]}]} 171 5:::{"name":"message","args":[{"fromUserName":"tester","fromUserId":"123","text":"heiiiii this is going to trigger a message for u!","threadId":"heacrsi1","messageId":1}]} 2013-03-16 05:11:45.493 [833:907] start/reset timeout 2013-03-16 05:11:45.495 [833:907] disconnect 2013-03-16 05:11:45.496 [833:907] onDisconnect() 

I can reproduce this problem sequentially. Is it normal that the data is "merged"? Why is this shutdown happening?

EDIT: I was able to simplify my problems into something very simple:

This piece of code is in order:

  socket.on('online', function(data){ socket.emit("message", {"testField":"testData2"}); }); 

This piece of code disconnects the client !:

  socket.on('online', function(data){ socket.emit("message", {"testField":"testData"}); socket.emit("message", {"testField":"testData2"}); }); 

Is it possible to constantly release something into the outlet? Do I have to implement some kind of queue myself to ensure that each socket is successful before I emit the following data?

===== UPDATE =====

p / s 1: This only happens on the objective-c client. If I use the javascript client, I can get two events.

p / s 2: I was able to reproduce the problem in a very simple setup: a. Firstly, a server that simply emits two connection events: io.sockets.on ('connection', function (socket) {socket.emit ("message", {"text": "welcome2!"}); Socket .emit ("message", {"text": "welcome3!"});} b. Secondly, a simple iOS client (using the socket.IO-obj library here: https://github.com/pkyeck/socket .IO-objc )

 - (void) viewDidLoad { [super viewDidLoad]; socketIO = [[SocketIO alloc] initWithDelegate:self]; [socketIO connectToHost:@"192.168.1.87" onPort:5000 withParams:@{@"token":@"avalidtoken"}]; } 

with. output from iOS client:

  2013-03-21 01:13:39.355 SocketTesterARC[6391:907] Connecting to socket with URL: http://192.168.1.87:5000/socket.io/1/?t=16807&token=avalidtoken 2013-03-21 01:13:39.620 SocketTesterARC[6391:907] didReceiveResponse() 200 2013-03-21 01:13:39.621 SocketTesterARC[6391:907] connectionDidFinishLoading() fvSZFJMiIXop5uMayU0t:60:60:xhr-polling 2013-03-21 01:13:39.622 SocketTesterARC[6391:907] sid: fvSZFJMiIXop5uMayU0t 2013-03-21 01:13:39.656 SocketTesterARC[6391:907] heartbeatTimeout: 67.000000 2013-03-21 01:13:39.657 SocketTesterARC[6391:907] transports: ( "xhr-polling" ) 2013-03-21 01:13:39.658 SocketTesterARC[6391:907] xhr polling supported -> using it now 2013-03-21 01:13:39.680 SocketTesterARC[6391:907] onData 1:: 2013-03-21 01:13:39.681 SocketTesterARC[6391:907] start/reset timeout 2013-03-21 01:13:39.683 SocketTesterARC[6391:907] connected 2013-03-21 01:13:39.684 SocketTesterARC[6391:907] onConnect() 2013-03-21 01:13:39.685 SocketTesterARC[6391:907] connected to server successfully 2013-03-21 01:13:39.686 SocketTesterARC[6391:907] doQueue() >> 0 2013-03-21 01:13:39.687 SocketTesterARC[6391:907] start/reset timeout 2013-03-21 01:13:39.698 SocketTesterARC[6391:907] onData  52 5:::{"name":"message","args":[{"text":"welcome2!"}]} 52 5:::{"name":"message","args":[{"text":"welcome3!"}]} 2013-03-21 01:13:39.700 SocketTesterARC[6391:907] start/reset timeout 2013-03-21 01:13:39.701 SocketTesterARC[6391:907] disconnect 2013-03-21 01:13:39.702 SocketTesterARC[6391:907] onDisconnect() 2013-03-21 01:13:39.708 SocketTesterARC[6391:907] disconnected! error: Error Domain=SocketIOError Code=-2 "The operation couldn't be completed. (SocketIOError error -2.)" 2013-03-21 01:13:44.687 SocketTesterARC[6391:907] disconnect! 
+4
source share
1 answer

After a little digging, there is a problem with how socket.io combines several messages into one package.

Two questions ( # 65 # 83 ) depict problems and discuss the problem in detail.

Thus, the socket.IO-objc library did not handle these special cases and always assumed that the packet contained only one message.

Problem number 65 for reference:

Every once in a while (with intensive socket traffic), the server may decide to send a payload when several packets are returned in response to a single poll (for example, when using an xhr poll). They are separated by the \ ufffd character, and include the byte length of each packet, for example:

  [packet_0 length] [packet_0] [packet_1 length] [packet_1] [packet_n 

length] [packet_n]

Currently, I believe that onData simply processes the data as a single packet, but there are times when the server sends multiple packets in a single response.

A note is a \ufffd .

A corrected earlier and looks like a review of this publication.

+2
source

Source: https://habr.com/ru/post/1469369/


All Articles