nope/lib/communication/socketBackend.ts

27 lines
727 B
TypeScript

import { CommunicationEvents, ICommunicationInterface } from "../dispatcher/nopeDispatcher";
import * as io from 'socket.io';
import { Server } from "http";
const server = require('http').createServer();
export class SocketBackend implements ICommunicationInterface{
protected _socket: io.Server
constructor(public port: number, server?: Server){
if (server){
this._socket = (io as any)(server);
} else {
this._socket = (io as any)();
}
this._socket.listen(port);
}
on(event: CommunicationEvents, cb: (...args: any[]) => void) {
this._socket.on('event', cb);
}
send(data: any): void {
this._socket.emit(data);
}
}