change online SocketEvent and add onlineCounter subscriber back to APISocketServer

This commit is contained in:
HF 2022-01-12 02:23:28 +01:00
parent 6dc7f21555
commit 031de42878
4 changed files with 50 additions and 35 deletions

19
API.md
View File

@ -12,7 +12,11 @@ Authorization: "Bearer APISOCKETKEY"
``` ```
All requests are made as JSON encoded array. All requests are made as JSON encoded array.
### Subscribe to chat messages ### Subscribe to chat messages
send
```["sub", "chat"]``` ```["sub", "chat"]```
All chat messages, except the once you send with `chat` or `mcchat`, will be sent to you in the form: All chat messages, except the once you send with `chat` or `mcchat`, will be sent to you in the form:
@ -21,13 +25,23 @@ All chat messages, except the once you send with `chat` or `mcchat`, will be sen
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come. channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.
id is the user id id is the user id
country is the [two-letter country code](https://www.nationsonline.org/oneworld/country_code_list.htm) in lowercase country is the [two-letter country code](https://www.nationsonline.org/oneworld/country_code_list.htm) in lowercase
### Online user counter
Online counter will be sent to you as typical binary package (see `src/socket/packets/OnlineCounter.js`) ### Subscribe to online user counter
send
```["sub", "online"]```
Online counter will be sent to you all 10s as typical binary package (see `src/socket/packets/OnlineCounter.js`)
### Subscribe to pixel packages ### Subscribe to pixel packages
send
```["sub", "pxl"]``` ```["sub", "pxl"]```
All pixels (including your own) will be sent to you as typical binary packages All pixels (including your own) will be sent to you as typical binary packages
### Set Pixel ### Set Pixel
```[ "setpxl", minecraftid, ip, x, y, clr ]``` ```[ "setpxl", minecraftid, ip, x, y, clr ]```
@ -49,6 +63,7 @@ waitSeconds is the current cooldown.
coolDownSeconds is the added cooldown (negative if pixel couldn't be set because max cooldown got reached) coolDownSeconds is the added cooldown (negative if pixel couldn't be set because max cooldown got reached)
### Send Chat Message ### Send Chat Message
```["chat", name, message, country, channelId]``` ```["chat", name, message, country, channelId]```
channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come. channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come.

View File

@ -61,6 +61,7 @@ class APISocketServer {
ws.isAlive = true; ws.isAlive = true;
ws.subChat = false; ws.subChat = false;
ws.subPxl = false; ws.subPxl = false;
ws.subOnline = false;
ws.on('pong', heartbeat); ws.on('pong', heartbeat);
ws.on('message', (data, isBinary) => { ws.on('message', (data, isBinary) => {
@ -72,11 +73,13 @@ class APISocketServer {
}); });
this.broadcast = this.broadcast.bind(this); this.broadcast = this.broadcast.bind(this);
this.broadcastOnlineCounter = this.broadcastOnlineCounter.bind(this);
this.broadcastPixelBuffer = this.broadcastPixelBuffer.bind(this); this.broadcastPixelBuffer = this.broadcastPixelBuffer.bind(this);
this.ping = this.ping.bind(this); this.ping = this.ping.bind(this);
this.broadcastChatMessage = this.broadcastChatMessage.bind(this); this.broadcastChatMessage = this.broadcastChatMessage.bind(this);
socketEvents.on('broadcast', this.broadcast); socketEvents.on('broadcast', this.broadcast);
socketEvents.on('onlineCounter', this.broadcastOnlineCounter);
socketEvents.on('pixelUpdate', this.broadcastPixelBuffer); socketEvents.on('pixelUpdate', this.broadcastPixelBuffer);
socketEvents.on('chatMessage', this.broadcastChatMessage); socketEvents.on('chatMessage', this.broadcastChatMessage);
@ -104,11 +107,13 @@ class APISocketServer {
}); });
} }
broadcast(data) { broadcast(data, filter = null) {
if (typeof data === 'string') { if (typeof data === 'string') {
this.wss.clients.forEach((ws) => { this.wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) { if (ws.readyState === WebSocket.OPEN) {
ws.send(data); if (!filter || filter(ws)) {
ws.send(data);
}
} }
}); });
} else { } else {
@ -121,40 +126,31 @@ class APISocketServer {
}); });
this.wss.clients.forEach((ws) => { this.wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) { if (ws.readyState === WebSocket.OPEN) {
frame.forEach((buffer) => { if (!filter || filter(ws)) {
try { frame.forEach((buffer) => {
// eslint-disable-next-line no-underscore-dangle try {
ws._socket.write(buffer); // eslint-disable-next-line no-underscore-dangle
} catch (error) { ws._socket.write(buffer);
logger.error(`WebSocket broadcast error: ${error.message}`); } catch (error) {
} logger.error(`WebSocket broadcast error: ${error.message}`);
}); }
});
}
} }
}); });
} }
} }
broadcastOnlineCounter(data) {
this.broadcast(data, (client) => client.subOnline);
}
broadcastPixelBuffer(canvasId, chunkid, buffer) { broadcastPixelBuffer(canvasId, chunkid, buffer) {
if (canvasId !== 0 && canvasId !== '0') return; // just canvas 0 for now
const frame = WebSocket.Sender.frame(buffer, { if (canvasId !== 0 && canvasId !== '0') {
readOnly: true, return;
mask: false, }
rsv1: false, this.broadcast(buffer, (client) => client.subPxl);
opcode: 2,
fin: true,
});
this.wss.clients.forEach((client) => {
if (client.subPxl && client.readyState === WebSocket.OPEN) {
frame.forEach((data) => {
try {
// eslint-disable-next-line no-underscore-dangle
client._socket.write(data);
} catch (error) {
logger.error('(!) Catched error on write apisocket:', error);
}
});
}
});
} }
async onTextMessage(message, ws) { async onTextMessage(message, ws) {
@ -169,9 +165,12 @@ class APISocketServer {
const even = packet[0]; const even = packet[0];
if (even === 'chat') { if (even === 'chat') {
ws.subChat = true; ws.subChat = true;
} } else if (even === 'pxl') {
if (even === 'pxl') {
ws.subPxl = true; ws.subPxl = true;
} else if (even === 'online') {
ws.subOnline = true;
} else {
logger.info(`APISocket wanted to sub to unexisting ${command}`);
} }
logger.info(`APISocket client subscribed to ${command}`); logger.info(`APISocket client subscribed to ${command}`);
return; return;

View File

@ -154,7 +154,7 @@ class SocketEvents extends EventEmitter {
broadcastOnlineCounter(online) { broadcastOnlineCounter(online) {
this.onlineCounter = online; this.onlineCounter = online;
const buffer = OnlineCounter.dehydrate(online); const buffer = OnlineCounter.dehydrate(online);
this.emit('broadcast', buffer); this.emit('onlineCounter', buffer);
} }
} }

View File

@ -120,6 +120,7 @@ class SocketServer {
this.onlineCounterBroadcast = this.onlineCounterBroadcast.bind(this); this.onlineCounterBroadcast = this.onlineCounterBroadcast.bind(this);
socketEvents.on('broadcast', this.broadcast); socketEvents.on('broadcast', this.broadcast);
socketEvents.on('onlineCounter', this.broadcast);
socketEvents.on('pixelUpdate', this.broadcastPixelBuffer); socketEvents.on('pixelUpdate', this.broadcastPixelBuffer);
socketEvents.on('reloadUser', this.reloadUser); socketEvents.on('reloadUser', this.reloadUser);