const WebSocket = require('ws');
const ws = new WebSocket('wss://api.erlcrussia.com/v2/gateway');
ws.on('open', () => {
// 1. Autenticazione
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('Connesso, clientId:', msg.clientId);
break;
case 5128:
console.log('Autenticazione riuscita');
// 2. Iscrizione ai canali
ws.send(JSON.stringify({
action: 'subscribe',
channels: ['Server', 'Players', 'Queue', 'JoinLogs']
}));
break;
case 5131:
console.log('Iscrizioni attive:', msg.channels);
break;
case 5130:
console.warn('Canale negato:', msg.channel);
break;
case 5133:
console.log('Cache vuota');
break;
case 5129:
// pong
break;
default:
if (msg.code >= 5100) {
console.error('Errore:', msg.code, msg.message);
break;
}
// Aggiornamento dati
if (msg.Name) console.log('Server:', msg.Name, 'Giocatori:', msg.CurrentPlayers);
if (msg.Players) console.log('Giocatori:', msg.Players.length);
if (msg.Queue) console.log('Coda:', msg.Queue.length);
if (msg.JoinLogs) console.log('Log accessi:', msg.JoinLogs);
break;
}
});
ws.on('close', () => {
console.log('Connessione chiusa');
});
ws.on('error', (err) => {
console.error('Errore di connessione:', err.message);
});