tidwall / SwiftWebSocket

Fast Websockets in Swift for iOS and OSX
MIT License
1.53k stars 246 forks source link

No websocket close event #11

Closed zivelli closed 9 years ago

zivelli commented 9 years ago

I am using the 1.2 branch and everything seems to work well except I do not receive an event when there is a server disconnection

websocket.event.close = { (code, reason, clean) in println("web server socket close") }

event.message, event.open are working.

tidwall commented 9 years ago

If you see an open event, then you should see a close event once the underlying tcp connection has been terminated by the server. Either the server hasn't completely closed the socket, or you've encountered a bug.

Is this problem happening for you with the echo example in the README file?

Also, there's an end event that is guaranteed to fire regardless of connection state. There is also an error event that could provide some insight in what's happening.

Does the end and/or error events happen to fire?

zivelli commented 9 years ago

With the included echo example I can get the following results with tests

  1. If I haven't started the server The expected result is: error The operation couldn’t be completed. Connection refused
  2. Server is on: I can connect, send messages & receive messages
  3. If I quit the server: There is no close or error message

Which means I can perform any checks or reconnects as websocket thinks TCP is still open.

tidwall commented 9 years ago

Thanks. This is good information. I'll look into asap.

On a side note, I recommend moving the 2.0 version when you get the chance. It's actively supported and has some tremendous enhancements compared to 1.2.

zivelli commented 9 years ago

I also added the end event to the echo test example. I can verify that it fires when the func is first called but if I shut down the server I don't get an error, close or end trigger.

To test the server I used a TestChat app demoing SocketIO. If I shut down the server a Connection Closed event was fired.

tidwall commented 9 years ago

Yeah that sounds like a bug of some sort. Did you test with the Javascript SocketIO client?

zivelli commented 9 years ago

OK I'll look into the 2.0 version, does this mean a move to Xcode 7 & OSX 10.11? I running a few project in Xcode 6.4 and things are stable

zivelli commented 9 years ago

No I used this as a client test https://github.com/TeehanLax/SwiftSocketIODemo

tidwall commented 9 years ago

Yeah, the 2.0 version does require Xcode 7. :(

zivelli commented 9 years ago

OK, I must be doing something wrong. I've got Xcode 7 and I'm running the Echo Test. I can't get a close server or disconnected message to fire if I quit the server.

The close message is only fired if the server is not running, when I first run the echo test.

zivelli commented 9 years ago

Maybe I'm misunderstanding the sequence of triggered events. Will the 'close event' only fire after I init WebSocket

var ws = WebSocket("ws://localhost:90993")

Will it not fire like event.message?

tidwall commented 9 years ago

SwiftWebSocket events work just like the JS WebSocket version, with the addition of the end convenience event.

error: Fires when an error is encountered. open: Fires only when a valid websocket connection is made with the server. message: Fires messages close: Fires only when a valid websocket connection is made, and the server sends a close message or when the tcp connection terminates. end: Fires when the WebSocket object is no longer in use. This will always fire once and will always be the last event to fire.

At a minimum you should see an open event or an error event describing the reason for the failure. You will also see the end event. You will only see the open and close event if a valid connection was made to the server.

tidwall commented 9 years ago
var ws = WebSocket("ws://localhost:90993")

Will attempt to open a connection with the server. If successful the event.message will handle the server messages.

The event.close will fire when the server sends a close message or the TCP terminates.

tidwall commented 9 years ago

I'm looking into the problem right now. I'll keep you posted.

tidwall commented 9 years ago

I just pushed an update to master. This should address the problem that you are seeing. Let me know if it does. This is for the 2.0 version.

zivelli commented 9 years ago

I can verify that the close event is firing when I drop the connection.

Thanks so much!

tidwall commented 9 years ago

No problem. And thanks for your help too.