Realtime API Ignoring my emits?

So I am trying to interface with the real-time API for a project I am working on. Im using python and the python-socketio library. I’ve browsed around and at this point my proof of concept for just trying to get some data back / make a move in a test game looks like this:

import socketio
import requests

token = ""

sio = socketio.Client(logger=True)

auth = requests.get('https://online-go.com/api/v1/ui/config', headers={'Authorization': f'Bearer {token}'}).json()

sio.connect('https://online-go.com/socket.io/?EIO=4', transports='websocket', headers={"Authorization" : f"Bearer {token}"})
sio.emit("notification/connect", data={"auth": auth['notification_auth'], "player_id": 1010740, "username": "diobolic"})
sio.emit("chat/connect", data={"auth": auth['chat_auth'], "player_id": 1010740, "username": "diobolic"})
sio.emit("authenticate", data={"auth": auth['chat_auth'], "player_id": 1010740, "username": "diobolic"})
sio.emit('game/connect', data={'game_id': 51320670, 'player_id': 1010740, 'chat': False})
sio.emit('game/move', data={'game_id': 51320670, 'player_id': 1010740, 'move': "k10"})

To test for now, I am just grabbing my Bearer token through postman. But when I run this script, I can auth and get my chat token and notification token, but this is the only output I get.

Engine.IO connection established
Namespace / is connected
Emitting event "notification/connect" [/]
Emitting event "chat/connect" [/]
Emitting event "authenticate" [/]
Emitting event "game/connect" [/]
Emitting event "game/move" [/]
Received event "active-bots" [/]
Engine.IO connection dropped
Connection failed, new attempt in 0.70 seconds

My understanding is that game/connect should give me back game data, but I dont see any events coming from the websocket besides active-bots. Is there something I am missing, or is it just ignoring my emits?

I think you are missing the jwt in the authenticate step.

I used the code in this repo to connect to game chat a few years back:

2 Likes

Thanks for the reply! Unfortunately, I don’t think that’s the issue. I added it like so:

sio.emit("authenticate", data={"auth": auth['chat_auth'], "player_id": 1010740, "username": "diobolic", "jwt": auth['user_jwt']})

And im still not getting any responses from the websocket. I enabled engineio_logger aswell and can see a few things:

  • I am connecting to the socket successfully:
Attempting WebSocket connection to wss://online-go.com/socket.io/?EIO=4&transport=websocket&EIO=4
WebSocket connection accepted with {'sid': '$ID', 'upgrades': [], 'pingInterval': 25000, 'pingTimeout': 20000, 'maxPayload': 1000000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Received packet MESSAGE data 0
Namespace / is connected

Sending any other message is completely ignored:

Emitting event "notification/connect" [/]
Sending packet MESSAGE data 2["notification/connect",{"auth":"$TOKEN","player_id":1010740,"username":"diobolic"}]

The authenticate emit also doesn’t net me a response:

Emitting event "authenticate" [/]
Sending packet MESSAGE data 2["authenticate",{"auth":"$JWT","player_id":1010740,"username":"diobolic","jwt":"$JWT"}]

Then, I just get the active-bots message, and it closes the connection:

Received packet MESSAGE data 2["active-bots",{"58441":{"id":58441,"country":"un","username":"GnuGo","icon-url":"https://secure.gravatar.com/avatar/f94fc0308de91aa099afa1db1afd8e44?s=32&d=retro","ui_class":"bot","ratings":{"version":5,"overall":......

I seem to get this empty packet error, then it closes out

Unexpected error decoding packet: "Invalid empty packet received", aborting
Waiting for write loop task to end
Exiting write loop task
Engine.IO connection dropped
Connection failed, new attempt in 0.80 seconds
Exiting read loop task
Attempting WebSocket connection to wss://online-go.com/socket.io/?EIO=4&transport=websocket&EIO=4

I even tried using a catch-all event handler to print out any messages I am getting, and the active-bots is still the only one:

@sio.on('*')
def catch_all(event, data):
  print(event)
  print(data)

Its entirely possible I am just using this socketio library incorrectly.

1 Like

I looked through your script to see if there was anything obvious I was missing, and noticed how you call the authenticate emit on connect, so I added this, and I am still not seeing any response saying I am authenticated, or any response besides the bots message. I also might have thought it was a socket.io namespace thing, so I matched your namespace, and got the same result.

@sio.on('connect')
def authenticate():
  sio.emit(event="authenticate", data={"auth": auth['chat_auth'], "player_id": 1010740, "username": "Diobolic", "jwt": auth['user_jwt']}, namespace=None)

As I recall, it doesn’t really send messages back, so it might be working correctly.

1 Like

The script up top should be sending a move to a game I have active, but it doesn’t seem to be. And it still looks like it’s just disconnecting after sending the bots message. The documentation claims that when I send the game connect message, I should receive Metadata about the game, but it’s not doing that either. Though that could just be outdated api documentation

1 Like

I remember a thread where someone’s moves were being sent before the server authenticated. Maybe try adding sleep statements? Iirc, 1 second between every call was sufficient.

Edit: What kind of events do reviews listen to and emit - #14 by zoodiabloc

1 Like

Man there is tons of inside baseball hidden in the forum that I seem to have a hard time finding :sweat_smile: but not giving Auth enough time would make sense. I’ll give that a try here shortly.

1 Like

So I’m not sure that I am even maintaining a stable connection to the socket. I tried adding in the sleep’s to give some breathing room, but it just kept retrying the authenticate emit.

So, to eliminate as many variables as possible, I ran this bit of code manually:

>>> sio = socketio.Client(logger=True, engineio_logger=False)
>>> 
>>> auth = requests.get('https://online-go.com/api/v1/ui/config', headers={'Authorization': f'Bearer {token}'}).json()
>>> sio.connect('https://online-go.com/socket.io/?EIO=4', transports='websocket', headers={"Authorization" : f"Bearer {token}"})

Im not even emitting any other events, the connection just keeps dropping after getting active-bots:

Engine.IO connection established
Namespace / is connected
>>> Received event "active-bots" [/]
Engine.IO connection dropped
Connection failed, new attempt in 0.57 seconds
Engine.IO connection established
Namespace / is connected
Reconnection successful
Received event "active-bots" [/]
Engine.IO connection dropped
Connection failed, new attempt in 1.44 seconds
Engine.IO connection established
Namespace / is connected
Reconnection successful
Received event "active-bots" [/]
Engine.IO connection dropped

SOLVED: This is due to a reverse compatibility issue with python-socketio. I was using python-socketio version 5, when what I needed was to be using version 4, since OGS seem to be using an older version of EIO. Once I got that version installed, I am now getting proper responses from the server.

What tipped me off was when I enabled engineio logging, I saw it was upset about an unknown empty packet:

Unexpected error decoding packet: "Invalid empty packet received", aborting

So my client was aborting the connection due to the heartbeat that the server was sending upon connection.

Installed the module using compatible versions, and I get good output now:

requirements.txt for python

python-engineio==3.13.2
python-socketio==4.6.0

output:

Received event "active-bots" [/]
Received packet PONG data None
Emitting event "hostinfo" [/]
Sending packet MESSAGE data 2["hostinfo"]
Received packet MESSAGE data 2["hostinfo",{"hostname":"ogs-termserver-549f7c9f86-ld8nx","clients":0,"uptime":32317.735,"ggs-version":"rengo-1.2.2"}]
4 Likes