From cdc5bf61e20207e4fe3c6a3e56c1989da0c50537 Mon Sep 17 00:00:00 2001 From: vijayabhaskarev Date: Sat, 8 Jun 2024 23:37:11 +0530 Subject: [PATCH 1/3] Implemented functionality for custom events --- lib/enums.ts | 4 ++++ lib/peer.ts | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/enums.ts b/lib/enums.ts index 0394f90aa..62d75b975 100644 --- a/lib/enums.ts +++ b/lib/enums.ts @@ -58,6 +58,10 @@ export enum PeerErrorType { * Native WebRTC errors. */ WebRTC = "webrtc", + /** + * The payload is not a valid JSON + */ + InvalidJson = "Invalid JSON", } export enum BaseConnectionErrorType { diff --git a/lib/peer.ts b/lib/peer.ts index 6fa306cbc..6211b22d2 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -19,7 +19,6 @@ import type { import { BinaryPack } from "./dataconnection/BufferedConnection/BinaryPack"; import { Raw } from "./dataconnection/BufferedConnection/Raw"; import { Json } from "./dataconnection/BufferedConnection/Json"; - import { EventEmitterWithError, PeerError } from "./peerError"; class PeerOptions implements PeerJSOption { @@ -106,6 +105,8 @@ export interface PeerEvents { * Errors from the underlying socket and PeerConnections are forwarded here. */ error: (error: PeerError<`${PeerErrorType}`>) => void; + + [eventName: string]: any; } /** * A peer who can initiate connections with other peers. @@ -449,6 +450,18 @@ export class Peer extends EventEmitterWithError { } else if (connectionId) { // Store for possible later use this._storeMessage(connectionId, message); + } + + /**For listening to custom events */ + if(typeof type === 'string') { + try{ + const data = JSON.parse(payload); + this.emit(type, data); + }catch(err){ + this.emitError(PeerErrorType.InvalidJson,"Invalid JSON") + logger.warn("Invalid JSON:", err) + } + } else { logger.warn("You received an unrecognized message:", message); } @@ -741,4 +754,11 @@ export class Peer extends EventEmitterWithError { .then((peers) => cb(peers)) .catch((error) => this._abort(PeerErrorType.ServerError, error)); } + + + _emit(type, payload) { + this.socket.send({ type, payload }); + } + + } From 656703b8d2492937cb4d125589a5b7763ec970d8 Mon Sep 17 00:00:00 2001 From: vijayabhaskarev Date: Sun, 9 Jun 2024 01:44:13 +0530 Subject: [PATCH 2/3] Fixed an issue in parsing the payload --- lib/enums.ts | 5 +---- lib/peer.ts | 11 ++--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/enums.ts b/lib/enums.ts index 62d75b975..d9839613e 100644 --- a/lib/enums.ts +++ b/lib/enums.ts @@ -58,10 +58,7 @@ export enum PeerErrorType { * Native WebRTC errors. */ WebRTC = "webrtc", - /** - * The payload is not a valid JSON - */ - InvalidJson = "Invalid JSON", + } export enum BaseConnectionErrorType { diff --git a/lib/peer.ts b/lib/peer.ts index 6211b22d2..641a7208e 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -453,15 +453,8 @@ export class Peer extends EventEmitterWithError { } /**For listening to custom events */ - if(typeof type === 'string') { - try{ - const data = JSON.parse(payload); - this.emit(type, data); - }catch(err){ - this.emitError(PeerErrorType.InvalidJson,"Invalid JSON") - logger.warn("Invalid JSON:", err) - } - + if(typeof type === 'string') { + this.emit(type, payload); } else { logger.warn("You received an unrecognized message:", message); } From 9b3d654be622060a581b2c9f281ff5aa388699b2 Mon Sep 17 00:00:00 2001 From: vijayabhaskarev Date: Sun, 9 Jun 2024 02:10:07 +0530 Subject: [PATCH 3/3] Fixed code formatting. --- lib/enums.ts | 1 - lib/peer.ts | 11 ++++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/enums.ts b/lib/enums.ts index d9839613e..0394f90aa 100644 --- a/lib/enums.ts +++ b/lib/enums.ts @@ -58,7 +58,6 @@ export enum PeerErrorType { * Native WebRTC errors. */ WebRTC = "webrtc", - } export enum BaseConnectionErrorType { diff --git a/lib/peer.ts b/lib/peer.ts index 641a7208e..cee9bbcf3 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -451,10 +451,10 @@ export class Peer extends EventEmitterWithError { // Store for possible later use this._storeMessage(connectionId, message); } - - /**For listening to custom events */ - if(typeof type === 'string') { - this.emit(type, payload); + + /**For listening to custom events */ + if (typeof type === "string") { + this.emit(type, payload); } else { logger.warn("You received an unrecognized message:", message); } @@ -748,10 +748,7 @@ export class Peer extends EventEmitterWithError { .catch((error) => this._abort(PeerErrorType.ServerError, error)); } - _emit(type, payload) { this.socket.send({ type, payload }); } - - }