2020-08-22 19:28:22 +02:00
|
|
|
import EventEmitter from "events";
|
2016-12-19 09:40:34 -05:00
|
|
|
|
2020-08-22 19:28:22 +02:00
|
|
|
const ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
|
|
|
|
const socket = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
|
2020-08-02 20:22:53 +02:00
|
|
|
|
2020-08-22 19:28:22 +02:00
|
|
|
const bus = new EventEmitter();
|
2018-07-21 16:53:53 +02:00
|
|
|
|
2020-08-22 19:28:22 +02:00
|
|
|
bus.on('log subscribe', () => {
|
|
|
|
|
socket.send(JSON.stringify({name: 'log subscribe'}));
|
|
|
|
|
});
|
2018-07-21 16:53:53 +02:00
|
|
|
|
2020-08-22 19:28:22 +02:00
|
|
|
bus.on('server status subscribe', () => {
|
|
|
|
|
socket.send(JSON.stringify({name: 'server status subscribe'}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bus.on('command send', command => {
|
|
|
|
|
socket.send(JSON.stringify({name: 'command send', data: command}));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.onmessage = e => {
|
|
|
|
|
const {name, data} = JSON.parse(e.data)
|
|
|
|
|
bus.emit(name, data);
|
2016-12-19 09:40:34 -05:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 19:28:22 +02:00
|
|
|
export default bus;
|