Files
factorio-server-manager/ui/api/socket.js

25 lines
663 B
JavaScript
Raw Normal View History

2020-08-22 19:28:22 +02:00
import EventEmitter from "events";
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();
2020-08-22 19:28:22 +02:00
bus.on('log subscribe', () => {
socket.send(JSON.stringify({name: 'log subscribe'}));
});
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);
}
2020-08-22 19:28:22 +02:00
export default bus;