nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

problem with emit #95

Closed lakamsani closed 8 years ago

lakamsani commented 8 years ago

Hi,

Trying to migrate a working iOS app using https://github.com/pkyeck/socket.IO-objc and 0.9.x socket.io on the server side (node.js 4.0.0) to: Socket.IO-Client-Swift (pod 'Socket.IO-Client-Swift', '~> 4.1.2') and socket.io@1.3.7 / socket.io-client@1.3.7 on the server side.

Created SocketIOClient like this:

[[SocketIOClient alloc] initWithSocketURL:hostString options:@{@"log": @YES, @"forcePolling": @NO, @"forceWebsockets":@YES}];

It connects find but emit does not work. Connect response on the iOS side looks like this:

Log SocketEngine: Got message: 0{"sid":"5IDRSgvt4IklWtcWAAAC","upgrades":[],"pingInterval":25000,"pingTimeout":60000} 2015-11-25 22:13:00.669 MyApp[33010:3096320] Log SocketEngine: Got message: 40 2015-11-25 22:13:00.669 MyApp[33010:3096320] Log SocketParser: Parsing 0 2015-11-25 22:13:00.670 MyApp[33010:3096320] Log SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}

On emit, iOS App prints logs like this:

2015-11-25 22:13:02.675 Onkore[33010:3096320] Log SocketIOClient: Emitting: 2[ correct JSON data being sent] 2015-11-25 22:13:02.675 Onkore[33010:3096304] Log SocketEngine: Writing ws: 2[correct JSON data being sent] 2015-11-25 22:13:02.676 Onkore[33010:3096304] Log SocketEngine: Sending ws: 2[correct JSON data being sent]

But the server does not seem to get the emit. On the iOS App side I see this repeating:

2015-11-25 22:13:26.044 MyApp[33010:3096301] Log SocketEngine: Writing ws: has data: 0 2015-11-25 22:13:26.045 MyApp[33010:3096301] Log SocketEngine: Sending ws: as type: 2 2015-11-25 22:13:26.046 MyApp[33010:3096392] Log SocketEngine: Got message: 3

Any thoughts?

nuclearace commented 8 years ago

You'll need to update the server to 1.0.0+ to use this client.

lakamsani commented 8 years ago

Right. As I mentioned above, I did switch the server to "socket.io@1.3.7 / socket.io-client@1.3.7 on the server side." That is the output of "npm list socket.io " and "socket.io-client" respectively on the server side.

nuclearace commented 8 years ago

My bad I read it wrong. I'll look more into it later today

lakamsani commented 8 years ago

No problem. I tried adding another socket.on handler corresponding to the emit event from my iOS app and connected to your sample server (after line 13) listed below and the emit went through. So some issue is posisble on my server too. I will update as I find more.

https://github.com/nuclearace/socket.io-client-swift-example/blob/master/server/index.js

lakamsani commented 8 years ago

got past the emit error by adjusting the format of a JSON object I 'm sending in that emit. I got the response back (playerLoginResult event) but the corresponding socket.on handler code is not being invoked.

2015-11-25 23:26:16.887 MyApp[35641:3190365] Log SocketParser: Decoded packet as: SocketPacket {type: 2; data: [playerLoginResult, 0, { ... some response JSON ...}]; id: -1; placeholders: -1; nsp: /}

2015-11-25 23:26:16.887 MyApp[35641:3190365] Log SocketIOClient: Handling event: playerLoginResult with data: ( 0, {... some response JSON ...})

[socket on:@"playerLoginResult" callback:^(NSArray* data, SocketAckEmitter* ack) { // some handler code. <---- NOT GETTING HERE after the above print out on the iOS App }];

lakamsani commented 8 years ago

Hi, got past that last error. Coding error on my part found via debugger. Had a addHandlers method that adds various event handlers but the addHandlers was never called! I had a "socket connected" printout for the "on" handler but the library was also printing "Socket connected" which I mistook to be my handler. The problem with the original emit not going out is due to JSON formatting error on my side. We can close this.

lakamsani commented 8 years ago

Anyone else that has a similar problem, the actual issue with emit is this:

  1. The code I inherited that was based on https://github.com/pkyeck/socket.IO-objc was converting the app's model objects into JSON strings first before sending it over WebSocket. using "(void) sendEvent:(NSString *)eventName withData:(id)data;" where data is a string.
  2. However when the string is sent via "[socket emit:event withItems:[NSArray arrayWithObject:[data]]]];" where data is a string, it was send as "{"name": "value"}" instead of {"name": "value"}. Note the extra quotes.

Fix is to not convert into a string and just send the dictionary. That is what the README.md says at https://github.com/pkyeck/socket.IO-objc although the API can accepts any 'id' and not just dictionaries. But for some reason the inherited code was sending strings directly and it seemed to work.