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.