Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Can't get close code #38

Closed
GavinPacini opened this issue Apr 4, 2016 · 7 comments
Closed

Can't get close code #38

GavinPacini opened this issue Apr 4, 2016 · 7 comments

Comments

@GavinPacini
Copy link

Hi there,

Many thanks for writing such a great library!

We were using the swift 1.2 version of your library and have recently updated to swift 2.1. I am using your swift 2.1 library but when our server sends a certain close code (4000-4016) your library seems to spit out 0. Is this normal?

Any help would be greatly appreciated.

Cheers,
Gavin

@tidwall
Copy link
Owner

tidwall commented Apr 4, 2016

Hi @GavinPacini,

This is not normal. When a server sends a close message that includes a code, SwiftWebSocket will close the connection on the app side and fire off the close and end events with the correct code. If the connection is closed or interrupted by the app or the server before the close message is received then the library will spit out Zero.

Are you calling .close() from the app? Or is the network connection getting closed prior to the server sending the message? Which event are you using to get the close code, .end or .close? Are there any errors being reported?

@tidwall
Copy link
Owner

tidwall commented Apr 13, 2016

I'm closing this issue for now. Please feel free to reopen if you feel that the issue is related to a bug with SwiftWebSocket or have further questions. Thanks.

@tidwall tidwall closed this as completed Apr 13, 2016
@GavinPacini
Copy link
Author

Hi @tidwall,

My apologies for not replying sooner. It seems that SwiftWebSocket was not reading the close frame properly. It works fine with Starscream and also works fine with an Android client.

Cheers,
Gavin

@tidwall tidwall reopened this Apr 13, 2016
@tidwall
Copy link
Owner

tidwall commented Apr 13, 2016

Shoot. Sorry for closing the issue prematurely. I tried to reproduce the issue, but to no avail. There must be a fringe case that is being missed, at least one that Autobahn does not test for. Could you provide some information regarding what is running on the server side?

@GavinPacini
Copy link
Author

No worries, my fault for not replying sooner, completely missed the notification!

On our server we are using Ratchet PHP (http://socketo.me/ & https://github.com/ratchetphp/Ratchet). I agree, it's strange the Autobahn tests are not picking it up! On our server side, we simply close the WebSocket connection using the standard Ratchet method and we pass it certain codes. These codes appear correctly on:

  • Starscream
  • Java-WebSocket
  • wscat

And also worked on your Swift/1.2 version.

I wish I could help more by explaining how Ratchet closes connections however I don't have the knowledge of Ratchet's internals!

I hope you can get this fixed! 😃

@tidwall
Copy link
Owner

tidwall commented Apr 13, 2016

Sweet. I think this is enough information. Hopefully it's a quick fix. Thanks a ton.

@tidwall
Copy link
Owner

tidwall commented Apr 14, 2016

I'm still not able to reproduce the issue. I created a Ratchet test server and was able to close the connection with a valid code. I'm guessing that I may not be closing the connection in the same manner as you.

To see what I'm seeing use the ratchet branch and try the steps below.

Start the Ratchet Server

# from the SwiftWebSocket directory
cd tools/ratchet
php server.php

This will start a websocket echo server on port 6790. The server receives messages from the client and echos the messages back. After the 5th message the server closes the connection with code 4012.

Run the iOS Simulator Client

I created a test app named Client that can run in the iOS simulator in the SwiftWebSocket Xcode project. You should see image in the build bar.

Running this will connect to the Ratchet server at ws://localhost:6790/echo and send messages. It will only be able to successfully send 5 messages before receiving a close event with the 4012 code.

opened
send: 1: 2016-04-14 14:24:38 +0000
recv: 1: 2016-04-14 14:24:38 +0000
send: 2: 2016-04-14 14:24:38 +0000
recv: 2: 2016-04-14 14:24:38 +0000
send: 3: 2016-04-14 14:24:38 +0000
recv: 3: 2016-04-14 14:24:38 +0000
send: 4: 2016-04-14 14:24:38 +0000
recv: 4: 2016-04-14 14:24:38 +0000
send: 5: 2016-04-14 14:24:38 +0000
recv: 5: 2016-04-14 14:24:38 +0000
send: 6: 2016-04-14 14:24:38 +0000
close 4012

Run the Javascript Client

There's a server tool that I wrote in Go for testing various cases. It includes a javascript client that performs the same actions as the iOS client.

The Ratchet server should still be running. From a second terminal run:

cd tools/server
./run_server.sh

Then from the web browser connect to http://localhost:6789/client?port=6790. This uses the Go server to serve the html/javascript client. The javascript will then connect to the Ratchet server at ws://localhost:6790/echo.

The following should appear in the browser window:

opened
send: 1: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
recv: 1: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
send: 2: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
recv: 2: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
send: 3: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
recv: 3: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
send: 4: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
recv: 4: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
send: 5: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
recv: 5: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
send: 6: Thu Apr 14 2016 07:46:06 GMT-0700 (MST)
close 4012

Both the iOS and Javascript clients see the 4012 code. Do you see something that I'm doing wrong? Is it possible to put together a simple example of the issue happening based on your code?

Thanks

@tidwall tidwall closed this as completed Aug 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants