UNPKG

node-jet

Version:

Jet Realtime Message Bus for the Web. Daemon and Peer implementation.

76 lines (75 loc) 2.42 kB
/* istanbul ignore file */ import { WebSocket as ws } from 'ws'; import MessageSocket from './message-socket.js'; import { isBrowser, isNodeJs } from './index.js'; /** Socket instance. * @class * @classdesc Class used as Interface to communicate between the native socket connection and the json rpc layer. */ export class Socket { id = ''; sock; type = ''; constructor(socket) { if (socket) { this.sock = socket; this.type = socket.constructor.name === 'MessageSocket' ? 'ms' : 'ws'; } } /** * Method to connect to Server * @param url url to connect to * @param ip Ip address to connect to only valid in combination with port * @param port Port used to connect */ connect = (url = undefined, ip = undefined, port = undefined) => { if (isBrowser) { this.sock = new WebSocket(url || `ws://${window.location.host}:${port || 2315}`, 'jet'); this.type = 'ws'; } else if (isNodeJs && url) { this.sock = new ws(url, 'jet'); this.type = 'ws'; } else { this.sock = new MessageSocket(port || 11122, ip); this.type = 'ms'; } }; /** * Closes the native socket connection */ close = () => { if (this.sock) this.sock.close(); }; /** * Listening to socket events to socket events * @param event "close": CloseEvent;"error": Event;"message": MessageEvent;"open": Event; * @param cb callback that is invoked when event is triggered * @emits CloseEvent in case of closing of the native socket * @emits Event in case of established socket connection * @emits MessageEvent in case of received message * @emits Event in case of error */ addEventListener = (event, cb) => { if ((this.type === 'ws' && isBrowser) || this.type === 'ms') { ; this.sock.addEventListener(event, cb); } else if (this.type === 'ws' && isNodeJs) { ; this.sock.addEventListener(event, cb); } else { throw Error('Could not detect socket type'); } }; /** * sending a message via the native socket * @param message //string that represents the message to send */ send = (message) => { this.sock?.send(message); }; }