@tashmet/server
Version:
``` npm install @tashmet/server ```
21 lines (20 loc) • 690 B
JavaScript
import { Server as SocketIOServer } from "socket.io";
import { TashmetNamespace } from "@tashmet/tashmet";
export default class Server {
constructor(storageEngine, serverOrPort) {
this.storageEngine = storageEngine;
this.io = new SocketIOServer(serverOrPort);
}
listen() {
this.io.on('connection', socket => {
socket.onAny((event, cmd, callback) => {
this.storageEngine.command(new TashmetNamespace(event), cmd).then(callback);
});
this.storageEngine.on('change', doc => socket.emit('change', doc));
});
}
close() {
this.io.disconnectSockets();
this.io.close();
}
}