socket.io-client-swift icon indicating copy to clipboard operation
socket.io-client-swift copied to clipboard

Can not connect after change device time

Open nthieu90 opened this issue 9 years ago • 12 comments

After change device time, Socket can not connect. This is logs:

SocketEnginePolling: Got polling response
SocketEnginePolling: Doing polling request
SocketEngine: Got message: 6

It repeats until receiving the message below and kills app: Message from debugger: Terminated due to memory issue

nthieu90 avatar Apr 25 '17 05:04 nthieu90

Event: error Description: SocketAnyEvent: Event: error items: Optional(["Got unknown error from server ok"]) Message from debugger: Terminated due to memory issue

nthieu90 avatar Apr 25 '17 09:04 nthieu90

Does it work after restarting the app after changing the times?

nuclearace avatar Apr 25 '17 10:04 nuclearace

I restart many time but it still not connect

nthieu90 avatar Apr 25 '17 10:04 nthieu90

If you can put the server into debug and see what requests it's doing that'd be helpful. The logs posted don't say much.

nuclearace avatar Apr 25 '17 10:04 nuclearace

I checked on socket.io-client-java and it works find.

nthieu90 avatar Apr 25 '17 10:04 nthieu90

I'm not sure how changing the device time could break this library unless there's a bug in URLSession or the WebSocket dependency.

nuclearace avatar Apr 25 '17 11:04 nuclearace

Hi @nuclearace, @nthieu90, server guy here.

First client issue a GET request to: /socket.io/?transport=polling&b64=1

server return a Set-Cookie header without MaxAge or Expire from https://github.com/socketio/engine.io/blob/81ef0bc939ed7f83a4c4b9df0fb2d313b9bf2cad/lib/server.js#L320 E.g:

HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 100
Set-Cookie: io=vKkfG9BFA44eSz7xAAxN; Path=/; HttpOnly
Date: Tue, 25 Apr 2017 13:30:36 GMT

Request logs for normal request:

  engine intercepting request for path "/socket.io/" +23s
  engine handling "GET" http request "/socket.io/?transport=polling&b64=1" +1ms
  engine handshaking client "MZK3FmCm7QlznrGKAAAB" +0ms
  engine:socket sending packet "open" ({"sid":"MZK3FmCm7QlznrGKAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}) +1ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "97:0{"sid":"MZK3FmCm7QlznrGKAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +0ms
  engine:socket executing batch send callback +1ms
  socket.io:server incoming connection with id MZK3FmCm7QlznrGKAAAB +2m
  socket.io:client connecting to namespace / +1ms
  socket.io:namespace adding socket to nsp / +0ms
  socket.io:socket socket connected - writing packet +0ms
  socket.io:socket joining room MZK3FmCm7QlznrGKAAAB +0ms
  socket.io:client writing packet {"type":0,"nsp":"/"} +0ms
  socket.io-parser encoding packet {"type":0,"nsp":"/"} +3.0m
  socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +0ms
  engine:socket sending packet "message" (0) +2ms
  socket.io:socket joined room MZK3FmCm7QlznrGKAAAB +1ms
  socket.io:client writing packet {"type":1,"nsp":"/"} +15s
  socket.io-parser encoding packet {"type":1,"nsp":"/"} +15s
  socket.io-parser encoded {"type":1,"nsp":"/"} as 1 +0ms
  engine:socket sending packet "message" (1) +15s
  socket.io:socket closing socket - reason server namespace disconnect +0ms
  socket.io:client ignoring remove for MZK3FmCm7QlznrGKAAAB +0ms
  socket.io:client forcing transport close +0ms
  socket.io:client client close with reason forced server close +0ms
  engine intercepting request for path "/socket.io/" +9s
  engine handling "POST" http request "/socket.io/?transport=polling&b64=1&sid=MZK3FmCm7QlznrGKAAAB" +0ms
  engine setting new request for existing client +0ms

logs for request from clients having time set too far away from present (more than 1 hour):

  engine intercepting request for path "/socket.io/" +1m
  engine handling "POST" http request "/socket.io/?transport=polling&b64=1&sid=NK2vgX-vmAGCmmqeAAAA" +1ms

there is no handshake for old clients - I think this more about the cookie expiration, it's still fine if the client time only few minutes different. It's working fine if the client's time is in the future.

If I try to "POST" to "/socket.io/?transport=polling&b64=1&sid=NK2vgX-vmAGCmmqeAAAA I get this error:

{
  "code": 1,
  "message": "Session ID unknown"
}

Any idea to fix this? I'm thinking about explicitly set maxAge for this cookie.

longkt90 avatar Apr 25 '17 14:04 longkt90

Is there any way can fix on client-side? As Android doesn't have this problem.

nthieu90 avatar Apr 25 '17 14:04 nthieu90

I'm not sure, I don't explicitly set the cookies. Unless they're given https://github.com/socketio/socket.io-client-swift/blob/master/Source/SocketEngine.swift#L196

nuclearace avatar Apr 25 '17 17:04 nuclearace

Before I changed time, I get this info:

  • LOG SocketEngine: Got message: 0{"sid":"PIBabljZiJpIh8PVAA9c","upgrades"["websocket"],"pingInterval":25000,"pingTimeout":60000}
  • LOG SocketEngine: Writing poll: has data: false
  • LOG SocketEnginePolling: Sending poll: as type: 2
  • SocketEnginePolling: Doing polling request
  • LOG SocketEngineClient: Connect
  • LOG SocketEnginePolling: Created POST string: 1:2
  • LOG SocketEnginePolling: POSTing
  • LOG SocketEnginePolling: Doing polling request
  • LOG SocketEnginePolling: Got polling response
  • LOG SocketEngine: Got message: 40
  • LOG SocketIOClient: Should parse message: 0
  • LOG SocketParser: Parsing 0
  • LOG SocketParser: Decoded packet as: SocketPacket {type: 0; data: []; id: -1; placeholders: 0; nsp: /}
  • LOG SocketIOClient: Socket connected

but after I changed time, I get the same sid

  • LOG SocketEnginePolling: Got polling response LOG SocketEngine: Got message: 0{"sid":"PIBabljZiJpIh8PVAA9c","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
  • LOG SocketEngine: Writing poll: has data: false
  • LOG SocketEnginePolling: Sending poll: as type: 2
  • LOG SocketEnginePolling: Doing polling request
  • LOG SocketEngineClient: Connect
  • LOG SocketEnginePolling: Created POST string: 1:2
  • LOG SocketEnginePolling: POSTing
  • LOG SocketEnginePolling: Doing polling request
  • LOG SocketEnginePolling: Got polling response
  • LOG SocketEnginePolling: Doing polling request
  • LOG SocketEngine: Got message: {"code":1,"message":"Session ID unknown"}
  • ERROR SocketEngine: Session ID unknown
  • ERROR SocketIOClient: Session ID unknown
  • LOG SocketIOClient: Handling event: error with data: ["Session ID unknown"]
  • LOG SocketEngine: Engine is being closed.
  • LOG SocketEnginePolling: Created POST string: 1:1
  • LOG SocketEnginePolling: Got polling response
  • LOG SocketEnginePolling: Doing polling request
  • LOG SocketIOClient: Starting reconnect
  • LOG SocketIOClient: Handling event: reconnect with data: ["Session ID unknown"]
  • LOG SocketIOClient: Trying to reconnect

And I try to restart app, it still get the dam sid but cannot connect

nthieu90 avatar Apr 26 '17 08:04 nthieu90

Clear cache and it can connect: URLCache.shared.removeAllCachedResponses()

nthieu90 avatar Apr 26 '17 10:04 nthieu90

Hi @nuclearace, I think the issue is that when we change the device time to the past, the system returns the cached result from previous call. May I suggest one fix in the library itself? Change line 194 in SocketEngine.swift to: let reqPolling = NSMutableURLRequest(url: urlPolling, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 0.0) We don't call connect often, so, I think it should be fine for not caching the request.

nhatminh12369 avatar Apr 26 '17 10:04 nhatminh12369