fetching + broadcasting dynamic data from RotClient-s
This commit is contained in:
@@ -6,6 +6,7 @@ export default class WebsocketManager {
|
||||
constructor() {
|
||||
this.clients = [];
|
||||
this.lastPing = {};
|
||||
this.initData = null;
|
||||
|
||||
setInterval(() => {
|
||||
const thr = Date.now() - 60000;
|
||||
@@ -18,10 +19,8 @@ export default class WebsocketManager {
|
||||
return {
|
||||
onOpen: (event, ws) => {
|
||||
this.joinClient(ws);
|
||||
ws.send(JSON.stringify("cau"));
|
||||
},
|
||||
onMessage(event, ws) {
|
||||
console.log(`Message from client: ${event.data}`)
|
||||
if (!event.data)
|
||||
return;
|
||||
|
||||
@@ -33,8 +32,10 @@ export default class WebsocketManager {
|
||||
_this.lastPing[ws] = Date.now();
|
||||
}
|
||||
|
||||
if (data.data)
|
||||
if (data.data) {
|
||||
_this.handleMessage(ws, data.data);
|
||||
console.log(`Data from client: ${event.data}`);
|
||||
}
|
||||
},
|
||||
onClose: (event, ws) => {
|
||||
this.leaveClient(ws);
|
||||
@@ -46,13 +47,18 @@ export default class WebsocketManager {
|
||||
|
||||
}
|
||||
|
||||
broadcast(message) {
|
||||
this.clients.forEach(client => client.send(JSON.stringify({data: message})));
|
||||
send(ws, data) {
|
||||
ws.send(JSON.stringify({data: data}));
|
||||
}
|
||||
|
||||
broadcast(data) {
|
||||
this.clients.forEach(client => this.send(client, data));
|
||||
}
|
||||
|
||||
joinClient(ws) {
|
||||
this.clients.push(ws);
|
||||
this.lastPing[ws] = Date.now();
|
||||
this.send(ws, this.initData)
|
||||
}
|
||||
|
||||
leaveClient(ws) {
|
||||
@@ -60,4 +66,8 @@ export default class WebsocketManager {
|
||||
delete this.lastPing[ws];
|
||||
}
|
||||
|
||||
setInitData(initData) {
|
||||
this.initData = initData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user