adding communication backends

This commit is contained in:
Martin Karkowski 2020-08-23 09:21:03 +02:00
parent 4f21b42013
commit 123495b654
3 changed files with 50 additions and 1 deletions

View File

@ -1,6 +1,9 @@
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { CommunicationEvents, ICommunicationInterface } from "../callDispatcher"; import { CommunicationEvents, ICommunicationInterface } from "../dispatcher/nopeDispatcher";
/**
* Event Layer.
*/
export class EventLayer implements ICommunicationInterface { export class EventLayer implements ICommunicationInterface {
protected _emitter = new EventEmitter(); protected _emitter = new EventEmitter();
on(event: CommunicationEvents, cb: (...args: any[]) => void) { on(event: CommunicationEvents, cb: (...args: any[]) => void) {

View File

@ -0,0 +1,27 @@
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);
}
}

View File

@ -0,0 +1,19 @@
import { CommunicationEvents, ICommunicationInterface } from "../dispatcher/nopeDispatcher";
import { Socket, connect } from 'socket.io-client';
export class SocketClient implements ICommunicationInterface{
protected _socket: typeof Socket;
constructor(public uri: string){
this._socket = connect(uri);
}
on(event: CommunicationEvents, cb: (...args: any[]) => void) {
this._socket.on('event', cb);
}
send(data: any): void {
this._socket.emit(data);
}
}