From 123495b654f4e6dea53f937fdbd09f3fb79b11e2 Mon Sep 17 00:00:00 2001 From: Martin Karkowski Date: Sun, 23 Aug 2020 09:21:03 +0200 Subject: [PATCH] adding communication backends --- lib/communication/eventLayer.ts | 5 ++++- lib/communication/socketBackend.ts | 27 +++++++++++++++++++++++++++ lib/communication/socketClient.ts | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/communication/socketBackend.ts create mode 100644 lib/communication/socketClient.ts diff --git a/lib/communication/eventLayer.ts b/lib/communication/eventLayer.ts index 6385033..d07f533 100644 --- a/lib/communication/eventLayer.ts +++ b/lib/communication/eventLayer.ts @@ -1,6 +1,9 @@ import { EventEmitter } from "events"; -import { CommunicationEvents, ICommunicationInterface } from "../callDispatcher"; +import { CommunicationEvents, ICommunicationInterface } from "../dispatcher/nopeDispatcher"; +/** + * Event Layer. + */ export class EventLayer implements ICommunicationInterface { protected _emitter = new EventEmitter(); on(event: CommunicationEvents, cb: (...args: any[]) => void) { diff --git a/lib/communication/socketBackend.ts b/lib/communication/socketBackend.ts new file mode 100644 index 0000000..271d2ff --- /dev/null +++ b/lib/communication/socketBackend.ts @@ -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); + } +} \ No newline at end of file diff --git a/lib/communication/socketClient.ts b/lib/communication/socketClient.ts new file mode 100644 index 0000000..91c6951 --- /dev/null +++ b/lib/communication/socketClient.ts @@ -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); + } +} \ No newline at end of file