This commit is contained in:
syuilo 2018-10-11 15:24:35 +09:00
parent e81f9a75dc
commit 030c1e6966
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69

View file

@ -1,9 +1,7 @@
import * as WebSocket from 'ws';
import from '../../ai'; import from '../../ai';
import IModule from '../../module'; import IModule from '../../module';
import serifs from '../../serifs'; import serifs from '../../serifs';
import config from '../../config'; import config from '../../config';
const ReconnectingWebSocket = require('reconnecting-websocket');
export default class ServerModule implements IModule { export default class ServerModule implements IModule {
public readonly name = 'server'; public readonly name = 'server';
@ -24,24 +22,8 @@ export default class ServerModule implements IModule {
this.ai = ai; this.ai = ai;
this.connection = new ReconnectingWebSocket(`${config.wsUrl}/server-stats`, [], { this.connection = this.ai.connection.useSharedConnection('serverStats');
WebSocket: WebSocket this.connection.on('stats', this.onStats);
});
this.connection.addEventListener('open', () => {
console.log('server-stats stream opened');
});
this.connection.addEventListener('close', () => {
console.log('server-stats stream closed');
this.connection._shouldReconnect && this.connection._connect()
});
this.connection.addEventListener('message', message => {
const msg = JSON.parse(message.data);
this.onConnectionMessage(msg);
});
setInterval(() => { setInterval(() => {
this.statsLogs.unshift(this.recentStat); this.statsLogs.unshift(this.recentStat);
@ -65,21 +47,8 @@ export default class ServerModule implements IModule {
} }
} }
private onConnectionMessage = (msg: any) => {
switch (msg.type) {
case 'stats': {
this.onStats(msg.body);
break;
}
default:
break;
}
}
private onStats = async (stats: any) => { private onStats = async (stats: any) => {
this.recentStat = stats; this.recentStat = stats.body;
} }
private warn = () => { private warn = () => {