Trouble connecting to the real time API

Hello there,

I am able to use the REST API smoothly to find games and stuff, but have trouble using to the Real Time API. I initially had the same problem as @the747beast in Opening a SocketIO object with https results in hang. I first tried using the socketIO-client for python like in the post, but then i was getting the same error: unexpected status code (400 {"code":0,"message":"Transport unknown"}).
As pointed out in the previous post, the problem is that the client is asking for /socket.io/?transport=polling while only websocket is supported. I wasn’t able to find a way to tell SocketIO to use websocket instead of polling, so as a ugly workaround I modified myself the package SocketIO-Client (replacing polling transport with websocket) and managed to finally connect… more or less.

I’m using this simple code:
with SocketIO('https://online-go.com') as sio: ...

and sometimes it connects, sometimes I get a Handshake status 502.

Any clues?

Note: I also tried the websocket package. A code like this always connects:
ws = websocket.WebSocketApp("wss://online-go.com/socket.io/?transport=websocket", on_message = on_message, on_error = on_error, on_close = on_close)
Yet the package doesn’t have any emit() built-in function and can only send strings like this ws.send(b'42["game/move", {"move": "ab", "auth": "1345567890", "game_id": 9024067, "player_id": 432818}]')

Update:
As using the websocket package looks more promising, I’ve created an interface for my bot based on it. I can use it to play a game and receive events from the real time API. But… I am regularly disconnected from the server. As a workaround, I recreate a connection every time I’m disconnected and then make sure everything is safe and synchronized between the bot and the server. But this is quite tricky and time consuming so I would rather keep the connection alive for hours.
The websocket connection is like this:

def on_message(ws, message):
     print("message", message)

def on_error(ws, error):
    print("error", error)

def on_close(ws):
    print("Disconnected!")

def on_open(ws):
    print("Connected!")

websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://online-go.com/socket.io/?transport=websocket", on_message=on_message, on_error=on_error, on_close=on_close)
ws.on_open = on_open
start = time.time()
ws.run_forever(ping_interval=25)
print('total time:', time.time() - start)

If I don’t define ping_interval, the connection will close after 30 seconds (exactly)
If I define a ping (whatever the value), the connection will close after 85 seconds (exactly)

Execution example:

--- request header ---
GET /socket.io/?transport=websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: online-go.com
Origin: http://online-go.com
Sec-WebSocket-Key: /xDWSAIfMMbKF2Bmfb/+ew==
Sec-WebSocket-Version: 13


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Connected!
Date: Sat, 10 Jun 2017 19:46:00 GMT
Connection: upgrade
Set-Cookie: __cfduid=d47c499ff361a5db3f34aaef0239c2b2a1497123960; expires=Sun, 10-Jun-18 19:46:00 GMT; path=/; domain=.online-go.com; HttpOnly
Upgrade: websocket
Sec-WebSocket-Accept: GhGAlOSxViNTGqKbUMiR54GpPeQ=
Server: cloudflare-nginx
CF-RAY: 36ced5912a066ecf-NRT
-----------------------
message 0{"sid":"BwPpNI9rCnuxNtBcAH_B","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
message 40
message 42["active-bots",{"58441":{"username":"GnuGo","ranking":21,"ranking_blitz":20,"ranking_live":21,"icon-url":"https://secure.gravatar.com/avatar/f94fc0308de91aa099afa1db1afd8e44?s=32&d=retro","country":"un","pro":0,"ui_class":"bot provisional","id":58441,"ranking_correspondence":21},"58542":{"username":"Fuego","ranking":25,"ranking_blitz":25,"ranking_live":25,"icon-url":"https://secure.gravatar.com/avatar/d1513ab3466031894dce2e9c0f0bafaa?s=32&d=retro","country":"un","pro":0,"ui_class":"bot provisional","id":58542,"ranking_correspondence":24},"130032":{"username":"Kugutsu","ranking":20,"ranking_blitz":17,"ranking_live":21,"icon-url":"https://b0c2ddc39d13e1c0ddad-93a52a5bc9e7cc06050c1a999beb3694.ssl.cf1.rackcdn.com/602541c97ea5d46acef2bafb1577bb29-32.png","country":"_DOOP","pro":0,"ui_class":"bot provisional","id":130032,"ranking_correspondence":21},"165325":{"username":"Nicaso","ranking":30,"ranking_blitz":29,"ranking_live":30,"icon-url":"https://b0c2ddc39d13e1c0ddad-93a52a5bc9e7cc06050c1a999beb3694.ssl.cf1.rackcdn.com/8f9655e00951d5a62abba4e7fbc077a2-32.png","country":"wf","pro":0,"ui_class":"bot provisional","id":165325,"ranking_correspondence":28},"419331":{"username":"RoyalLeela","ranking":34,"ranking_blitz":34,"ranking_live":34,"icon-url":"https://b0c2ddc39d13e1c0ddad-93a52a5bc9e7cc06050c1a999beb3694.ssl.cf1.rackcdn.com/8c28d578d14aa990796bcbbd59aa170c-32.png","country":"us","pro":0,"ui_class":"bot","id":419331,"ranking_correspondence":34},"434218":{"username":"DarkGo","ranking":33,"ranking_blitz":33,"ranking_live":33,"icon-url":"https://b0c2ddc39d13e1c0ddad-93a52a5bc9e7cc06050c1a999beb3694.ssl.cf1.rackcdn.com/3b8772d1ead443410d3bef349e75300f-32.png","country":"_Pirate","pro":0,"ui_class":"bot provisional","id":434218,"ranking_correspondence":31}}]
send: b'\x89\x80\x13\x9c,\xa3' <-- ping
send: b'\x89\x80rHHD'
send: b'\x89\x80keY6'
send: b'\x88\x82\x183W\xa5\x1b\xdb'
Disconnected!
total time: 85.67800807952881

Is there a reason behind these 30/85 seconds timeout?
The first message I receive says "pingInterval":25000,"pingTimeout":60000. Maybe it’s just a coincidence but 25000 + 60000 = 85000 = 85s? Then it would mean the server is supposed to ping me but fails and thus closes the connection.

2 Likes

I have a similar problem:

Whatever I do, the connection only lasts about 30 seconds - even if messages are actively send.

Before that happens I receive:
send: b’\x89\x80?\xef\xbf!’
and then
send: b’\x88\x82\xa8\x1f\x9f\xb0\xab\xf7’

in my terminal.

Does anyone has an explanation by now for this?
Thanks!