const WebSocket = require('ws');
const ws = new WebSocket('wss://api.erlcrussia.com/v2/gateway');
ws.on('open', () => {
// 1. Authentication
ws.send(JSON.stringify({
action: 'auth',
token: 'your_ws_key_here'
}));
});
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
switch (msg.code) {
case 5127:
console.log('Connected, clientId:', msg.clientId);
break;
case 5128:
console.log('Authentication successful');
// 2. Subscribe to channels
ws.send(JSON.stringify({
action: 'subscribe',
channels: ['Server', 'Players', 'Queue', 'JoinLogs']
}));
break;
case 5131:
console.log('Subscriptions:', msg.channels);
break;
case 5130:
console.warn('Denied channel:', msg.channel);
break;
case 5133:
console.log('Cache empty');
break;
case 5129:
// pong
break;
default:
if (msg.code >= 5100) {
console.error('Error:', msg.code, msg.message);
break;
}
// Data update
if (msg.Name) console.log('Server:', msg.Name, 'Players:', msg.CurrentPlayers);
if (msg.Players) console.log('Players:', msg.Players.length);
if (msg.Queue) console.log('Queue:', msg.Queue.length);
if (msg.JoinLogs) console.log('Join log:', msg.JoinLogs);
break;
}
});
ws.on('close', () => {
console.log('Connection closed');
});
ws.on('error', (err) => {
console.error('Connection error:', err.message);
});