How to make RT API connection (websocket)

Hello there !
Can someone demystify the basics RT API ?
As I konw right now, we start by request a token :

async function getAccessToken(username, password) {
  const uri = `https://online-go.com/oauth2/token/`
  const method = 'POST'
  const body = new FormData()
  body.append('grant_type', 'password')
  body.append('username', username)
  body.append('password', password)
  body.append('client_id', CLIENT_ID)
  body.append('client_secret', CLIENT_SECRET)
  const res = await fetch(uri, { method, body })
  return await res.json()
}

After this, we request the config object with the token :

async function getConfig(token) {
  const uri = `https://online-go.com/api/v1/ui/config`
  const headers = { 'Authorization': `Bearer ${token}` }
  const method = 'GET'
  const res = await fetch(uri, { method, headers })
  return await res.json()
}

So, I have right now those items :

  • token
  • chat_auth
  • user_jwt
  • csrf_token
  • user object (who is an anonyme unrelated with the user’s credentials)

Next I try to etablish websocket connection :

async function connectSocket(token, config) {
  const ws = new WebSocket('wss://online-go.com/socket.io/?transport=websocket')
  ws.on('open', () => {
    console.log("connected")
    ws.send(" ? ")
  })
  ws.on('message', (msg) => {
    console.log('message: ', msg)
  })
  ws.on('error', console.log)
}

I use the package ‘ws’ on npm. At this stage this is what I get :

connected
message:  0{"sid":"c7Uv9Pam43DrQ7BZAB59","upgrades":[],"pingInterval":25000,"pingTimeout":5000}

How should I send message to the api ? I tried differents way (formating, parsing…) but I can’t get the right combinaison.

ws.send(`42[“notification/connect”,{“player_id”:“***”,“username”:“***”,“auth”:“***”}] `)
ws.emit('notification/connect', {player_id, username, auth})
ws.emit('notification/connect', JSON.stringify({player_id, username, auth}))

And what auth value among all items above should I send ?
Could be usefull for all I guess, thank you !

Payload are json dictionaries.

Thank you, I feeling close to the solution but I’m not sure why I still doesn’t receive response :

Do I need to send binary data ? I suppose Ogs server use socket IO server with this emit helper. As I read a topic of walken, he mention that formating string (I updated it to the current context:

`42["authenticate",{"player_id":"${player_id}", "username": "${username}", "auth": "${auth}", "jwt": "${jwt}"}]`

Is it still the case ?
Because this don’t work :

const data = `42["authenticate",{"player_id":"${player_id}", "username": "${username}", "auth": "${auth}", "jwt": "${jwt}"}]`
    const opts = {}
    ws.send(data, opts)

Again thank you for you time :slight_smile:

EDIT: I could see that if I omit 42 at the beginning, I’ve got instantly disconnected. Otherwise, with any number at the start of the string, it disconnect the client after about 30s (which is normal, I read that client must ping regularly), what happend behind the scene ? :o

If You think ”42” is the problem, I’ve got bad news for You.
Show Us thee full source, It’ll be easier to help You.

A legend says that no one could solve that mystery!

Nothing more, nothing less:

import fetch from  'node-fetch'
import FormData from 'form-data'
import WebSocket from 'ws'

const CLIENT_ID = '...'
const CLIENT_SECRET = '...'
const USERNAME = 'MrBungle'
const PASSWORD = '...'

async function getAccessToken() {
  const uri = `https://online-go.com/oauth2/token/`
  const method = 'POST'
  const body = new FormData()
  body.append('grant_type', 'password')
  body.append('username', USERNAME)
  body.append('password', PASSWORD)
  body.append('client_id', CLIENT_ID)
  body.append('client_secret', CLIENT_SECRET)
  const res = await fetch(uri, { method, body })
  return await res.json()
}

async function getConfig(token) {
  const uri = `https://online-go.com/api/v1/ui/config`
  const headers = { 'Authorization': `Bearer ${token}` }
  const method = 'GET'
  const res = await fetch(uri, { method, headers })
  return await res.json()
}

async function connectSocket(player_id, username, auth, jwt) {
  const uri = 'wss://online-go.com/socket.io/?transport=websocket'
  const ws = new WebSocket(uri)
  ws.on('open', function open() {
    const data = `42["authenticate",{"player_id":"${player_id}", "username": "${username}", "auth": "${auth}", "jwt": "${jwt}"}]`
    ws.send(data)
  });
  ws.on('message', console.log)
  ws.on('error', console.log)
}

async function initialize() {
  const token = await getAccessToken()
  const config = await getConfig(token)
  const id = config.user.id
  const username = config.user.username
  const chat_auth = config.chat_auth
  const jwt = config.user_jwt
  connectSocket(id, username, chat_auth, jwt)
}

initialize()

What exactly are You trying to achieve?
It’s wrong stuff, dude. :frowning:

In general, just trying to understand how to talk with the real time api using ‘ws’ on nodejs environnement.
At this point, I would be happy if I can at least be authenticated. :partying_face:

Anybody can show me the good stuff ?
The flovo’s code is a good starting point but I would like to use node js…
Thanks

This is the OGS source (Transcript)

This is socket io on client side… I cannot make a connection on server side with this following code :

const uri = 'wss://online-go.com/socket.io/?transport=websocket'
const socket = io(uri)

So I used the ‘ws’ package and here I have this response :

0{"sid":"CzCoBj9mqs72mLiQBEtU","upgrades":[],"pingInterval":25000,"pingTimeout":5000}

From this topic (OGS API notes) the author say :

  • The client also sends an authenticate RT API call. This is necessary for the client to send game moves later on. The call is sent as 42[“authenticate”,{“player_id”:“your_id_here”,“username”:“your_username_here”,“auth”:“your_chat_auth”}]

You could see from above that I tried to follow thoses instructions without success.

const data = `42["authenticate",{"player_id":"${player_id}", "username": "${username}", "auth": "${auth}", "jwt": "${jwt}"}]`
ws.send(data)

I think my message don’t have the right structure and I need your expertise for this.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.