SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
291 stars 92 forks source link

Socket connection failed for unknown reasons #83

Closed JperF closed 6 years ago

JperF commented 7 years ago

I am having trouble debugging an issue that is causing the socket to be prematurely disconnected.

Steps to reproduce the problem:

  1. Open application
  2. Lock phone
  3. Unlock phone

Upon the application reloading I receive the following error.

image_uploaded_from_ios_720

XCode Debug message:

2017-06-19 10:17:48.570 [error][tid:com.facebook.react.JavaScript] Socket connection failed for unknown reasons
2017-06-19 10:17:48.589430-0700 iM[15460:3923025] Socket connection failed for unknown reasons
2017-06-19 10:17:48.598 [fatal][tid:com.facebook.react.ExceptionsManagerQueue] Unhandled JS Exception: Socket connection failed for unknown reasons
2017-06-19 10:17:48.600692-0700 iM[15460:3923037] Unhandled JS Exception: Socket connection failed for unknown reasons
2017-06-19 10:17:48.615316-0700 iM[15460:3923037] *** Terminating app due to uncaught exception 'RCTFatalException: Unhandled JS Exception: Socket connection failed for unknown reasons', reason: 'Unhandled JS Exception: Socket connection failed for unknown reasons, stack:
u@929:1722
_onSCClose@924:9359
<unknown>@924:4683
emit@727:1010
emit@925:490
_onClose@933:2321
onclose@933:1291
value@115:1505
<unknown>@126:2142
value@40:1281
value@20:2877
<unknown>@20:818
value@20:2278
value@20:790
'
*** First throw call stack:
(0x183486fe0 0x181ee8538 0x183486f28 0x1001e8a5c 0x1001e3150 0x18348ce80 0x1833822c4 0x183386e9c 0x1001e69b8 0x10020bd74 0x10020b724 0x1008d1a50 0x1008d1a10 0x1008df2e8 0x1008d5634 0x1008df810 0x1008e1630 0x1008e139c 0x182547100 0x182546cac)
libc++abi.dylib: terminating with uncaught exception of type NSException

This error does not happen on the simulator and only appears on an actual iphone.

Any suggestions??

jondubois commented 7 years ago

@JperF What version of socketcluster-client are you using? What version of socketcluster?

Are you calling socket.disconnect(...) somewhere?

"Socket connection failed for unknown reasons" means that the socket does not recognize the error code which was passed to it when closing.

Last time I saw this error, someone was trying to pass a string to disconnect like this: socket.disconnect('some custom error').

The first argument to the disconnect function should be an error code (a Number) and the second argument is the reason string (or you can call disconnect without any arguments). Search for the disconnect method here: http://socketcluster.io/#!/docs/api-scsocket-client

Also note that if you call socket.disconnect(...) with a custom error code and you do not provide a reason string as the second argument, then you will also get 'Socket connection failed for unknown reasons' because SC cannot match the custom code to any known SC error.

Let me know if this is caused by a different issue.

JperF commented 7 years ago

"socketcluster-client": "^5.5.1" "socketcluster": "5.x.x",

In our code we never directly call the disconnect function. I referenced a similar issue in a different library that suggested that you needed to disconnect your socket and reconnect it when an iOS application goes into the background.

After doing this with the use of AppState in react-native the crashing issue stopped but now when attempting to reconnect to the channels we defined before they aren't recognized correctly.

jondubois commented 7 years ago

Can you do npm ls socketcluster and also npm ls socketcluster-server when inside your project directory to get the exact version numbers?

JperF commented 7 years ago
└─┬ socketcluster@5.15.0
  └── socketcluster-server@5.14.0
└── socketcluster-client@5.5.1
jondubois commented 7 years ago

What do you mean by 'they aren't recognized correctly' are you seeing an error?

jondubois commented 7 years ago

Feel free to chat on Gitter if you need assistance: https://gitter.im/SocketCluster/socketcluster

jondubois commented 7 years ago

@JperF I pushed a fix with a small workaround to handle React Native having event.code being undefined (it will treat it as code 1005). socketcluster-client@5.5.2. Let me know if this fixes it.

Note that you will still get an error on the socket (since it is not a clean WebSocket close), but if you catch/handle it correctly with:

socket.on('error', function (err) {
  // Log error somewhere...
});

Then it shouldn't kill your RN process and SC should reconnect automatically.

JperF commented 7 years ago

@jondubois

Sounds good! Thanks so much for being so helpful and quick to respond!