sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.68k stars 1.66k forks source link

Connect times out on Android #114

Open jmglov opened 9 years ago

jmglov commented 9 years ago

websocket-sharp is working perfectly for me in the Unity editor and on iOS, but on Android, Connect() always times out before receiving a handshake. In the logs of my WebSockets server, I can see the Unity client connecting, and the handshake completes immediately from the server's point of view. On the Android side, however, my app hangs for awhile before finally reporting a timeout:

03-17 17:53:35.831  19580-19596/tv.rockscience.RockScience2 D/Unity﹕ 03/17/2015 17:53:35|FATAL|WebSocket.acceptException:0|WebSocketSharp.WebSocketException: A timeout has occurred while receiving a handshake.
            at WebSocketSharp.WsStream.ReadHandshake[HandshakeResponse] (System.Func`2 parser, Int32 millisecondsTimeout) [0x00000] in <filename unknown>:0
            at WebSocketSharp.WsStream.ReadHandshakeResponse () [0x00000] in <filename unknown>:0
            at WebSocketSharp.WebSocket.receiveHandshakeResponse () [0x00000] in <filename unknown>:0
            at WebSocketSharp.WebSocket.sendHandshakeRequest (WebSocketSharp.HandshakeRequest request) [0x00000] in <filename unknown>:0
            at WebSocketSharp.WebSocket.sendHandshakeRequest () [0x00000] in <filename unknown>:0
            at WebSocketSharp.WebSocket.doHandshake () [0x00000] in <filename unknown>:0
            at WebSocketSharp.WebSocket.connect () [0x00000] in <filename unknown>:0

I'm running Unity 4.6.3f1 Pro with both iOS and Android Pro, with a minimum Android API level of 4.0.3 and full .NET 2.0 compatibility level, using the websocket-sharp.dll from the Asset Store. I'm testing by exporting a development build as a Google Android Project and running it on my Android device with USB debugging from Android Studio.

Have you success reports from people using websocket-sharp on Android? I'm only using the client stuff, not the server.

jmglov commented 9 years ago

I have an update on this issue. I made a minimal Unity project to reproduce this (zip file here), and I found that I can connect to the WebSocket.org echo server on ws://echo.websocket.org.

However, I still cannot connect to my own server, which is based on the http-kit library for Clojure. I can connect to my server from the Unity editor, iOS, and every other websocket client I've tried, so there's something strange going on with the combination of websocket-sharp and http-kit, but only on Android. Ugh.

Can anyone suggest a good way to debug this? Can I turn on absurdly verbose logging in websocket-sharp? I suspect the issue is in the handshake process itself, as that is where I get the timeout.

jmglov commented 9 years ago

Here's a minimal WebSocket server that demonstrates the issue: https://drive.google.com/file/d/0B6lDe0twne6NekxUVzVkdUxPaHc/view?usp=sharing

Run it with java -jar ws-server-test.jar, and it will start listening for connections on ws://localhost:8080/. Updating the test Unity project to connect to port 8080 of your machine's IP address, building for Android, and running on a device will reproduce the error.

bnolan commented 9 years ago

I'm getting the same behaviour running against a node.js websocket server using "ws". Try connect to a SceneVR server to replicate. Did you make any discoveries?

bnolan commented 9 years ago

Well that's super weird. I can connect to the production servers that run node servers using ws behind nginx, but i cant connect directly to the node servers.

jmglov commented 9 years ago

@bnolan Sorry, I should have followed up here.

I fixed the issue by upgrading to Unity 5, turning on full .NET API compatibility , and dropping the websocket-sharp code directly in my Assets folder. Unity 4 could not build websocket-sharp like this, so I was using the DLL from the Asset Store.