diff --git a/example/host.html b/example/host.html index ce21f88..49f60ff 100644 --- a/example/host.html +++ b/example/host.html @@ -11,10 +11,11 @@ host.on("connection", function connection(ws) { ws.on("diagnostic", console.log); ws.on("message", function incoming(message) { - console.log("Host received: %s", message); + console.log("Host received: %s from %s", message, ws.remoteId); host.broadcast("Host received: "+ message); }); ws.on("open", function ready() { + console.log("Client %s connected", ws.remoteId); ws.send("something"); }); }); diff --git a/example/join.html b/example/join.html index 1bd611c..657e869 100644 --- a/example/join.html +++ b/example/join.html @@ -17,6 +17,7 @@ }); ws.on("diagnostic", console.log); ws.on("open", function open() { + console.log("Connected to host: %s", ws.remoteId); ws.send("hello"); }); ws.on("message", function incoming(message) { diff --git a/lib/RelaySocket.ts b/lib/RelaySocket.ts index 280e29a..3abb776 100644 --- a/lib/RelaySocket.ts +++ b/lib/RelaySocket.ts @@ -6,6 +6,7 @@ import { ClientSocketParams, HostSocketParams, Message } from "./models"; export class RelaySocket implements UhstSocket { private _ee = new EventEmitter(); private token: string; + private _remoteId: string; private relayMessageStream: MessageStream; private sendUrl?: string; @@ -18,11 +19,13 @@ export class RelaySocket implements UhstSocket { case "client": // will connect to host this.initClient(params.hostId); + this._remoteId = params.hostId; break; case "host": // client connected this.token = params.token; this.sendUrl = params.sendUrl; + this._remoteId = params.clientId; // give consumer a chance to subscribe to open event setTimeout(() => { this._ee.emit("open"); @@ -32,6 +35,9 @@ export class RelaySocket implements UhstSocket { throw Error("Unsupported Socket Parameters Type"); } } + get remoteId(): string { + return this._remoteId; + } on(eventName: EventName, handler: SocketEventSet[EventName]) { this._ee.on(eventName, handler); diff --git a/lib/UhstHost.ts b/lib/UhstHost.ts index 7c4e9ed..a9e6362 100644 --- a/lib/UhstHost.ts +++ b/lib/UhstHost.ts @@ -64,7 +64,7 @@ export class UhstHost { const clientId: string = (JwtDecode(message.responseToken) as any).clientId; let hostSocket = this.clients.get(clientId); if (!hostSocket) { - const socket = this.socketProvider.createUhstSocket(this.relayClient, {type: "host", token: message.responseToken, sendUrl: this.config.sendUrl}, this.debug); + const socket = this.socketProvider.createUhstSocket(this.relayClient, {type: "host", token: message.responseToken, sendUrl: this.config.sendUrl, clientId}, this.debug); if (this.debug) { this._ee.emit("diagnostic", "Host received client connection from clientId: "+clientId); } this._ee.emit("connection", socket); this.clients.set(clientId, socket); diff --git a/lib/WebRTCSocket.ts b/lib/WebRTCSocket.ts index f399074..39fe993 100644 --- a/lib/WebRTCSocket.ts +++ b/lib/WebRTCSocket.ts @@ -7,6 +7,7 @@ export class WebRTCSocket implements UhstSocket { private _ee = new EventEmitter(); private _pendingCandidates: (RTCIceCandidate | RTCIceCandidateInit)[] = []; private _offerAccepted = false; + private _remoteId; private token: string; private connection: RTCPeerConnection; private dataChannel: RTCDataChannel; @@ -31,16 +32,21 @@ export class WebRTCSocket implements UhstSocket { case "client": // will connect to host this.initClient(params.hostId); + this._remoteId = params.hostId; break; case "host": // will connect to client this.token = params.token; this.sendUrl = params.sendUrl; + this._remoteId = params.clientId; break; default: throw Error("Unsupported Socket Parameters Type"); } } + get remoteId(): string { + return this._remoteId; + } on(eventName: EventName, handler: SocketEventSet[EventName]) { this._ee.on(eventName, handler); diff --git a/lib/contracts/UhstSocket.ts b/lib/contracts/UhstSocket.ts index f41491a..edcbcf7 100644 --- a/lib/contracts/UhstSocket.ts +++ b/lib/contracts/UhstSocket.ts @@ -9,6 +9,8 @@ export type SocketEventSet = { } export interface UhstSocket { + readonly remoteId: string; + on(eventName: EventName, handler: SocketEventSet[EventName]); once(eventName: EventName, handler: SocketEventSet[EventName]); @@ -23,4 +25,5 @@ export interface UhstSocket { close(); handleMessage(message: Message); + } \ No newline at end of file diff --git a/lib/models/SocketParams.ts b/lib/models/SocketParams.ts index 1a316fc..cd2d400 100644 --- a/lib/models/SocketParams.ts +++ b/lib/models/SocketParams.ts @@ -1,6 +1,7 @@ export interface HostSocketParams { type: "host", token: string, + clientId: string, sendUrl?: string } diff --git a/test/RelaySocket.spec.ts b/test/RelaySocket.spec.ts index 3529443..688190a 100644 --- a/test/RelaySocket.spec.ts +++ b/test/RelaySocket.spec.ts @@ -51,6 +51,7 @@ describe("# RelaySocket", () => { const mockHostSocketParams: HostSocketParams = { type: "host", token: mockToken, + clientId: "testClient", sendUrl: "hostSendUrl" }; @@ -70,6 +71,7 @@ describe("# RelaySocket", () => { const mockHostSocketParams: HostSocketParams = { type: "host", token: mockToken, + clientId: "testClient", sendUrl: "hostSendUrl" }; @@ -101,6 +103,7 @@ describe("# RelaySocket", () => { const mockHostSocketParams: HostSocketParams = { type: "host", token: mockToken, + clientId: "testClient", sendUrl: "hostSendUrl" }; diff --git a/test/RelaySocketProvider.spec.ts b/test/RelaySocketProvider.spec.ts index 597e212..0a73777 100644 --- a/test/RelaySocketProvider.spec.ts +++ b/test/RelaySocketProvider.spec.ts @@ -24,6 +24,7 @@ describe("# RelaySocketProvider", () => { const provider = new RelaySocketProvider(); const mockHostSocketParams: HostSocketParams = { type: "host", + clientId: "testClient", token: "responseToken" }; expect(provider.createUhstSocket({}, mockHostSocketParams, false)).to.not.be.null;