Skip to content

Commit

Permalink
Merge pull request #11 from uhst/feat--add-remoteId-to-socket
Browse files Browse the repository at this point in the history
feat: expose remoteId on socket
  • Loading branch information
dimitrovs authored Aug 14, 2021
2 parents a65e7be + fdd5428 commit f0c83ac
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
Expand Down
1 change: 1 addition & 0 deletions example/join.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions lib/RelaySocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ClientSocketParams, HostSocketParams, Message } from "./models";
export class RelaySocket implements UhstSocket {
private _ee = new EventEmitter<SocketEventSet>();
private token: string;
private _remoteId: string;
private relayMessageStream: MessageStream;
private sendUrl?: string;

Expand All @@ -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");
Expand All @@ -32,6 +35,9 @@ export class RelaySocket implements UhstSocket {
throw Error("Unsupported Socket Parameters Type");
}
}
get remoteId(): string {
return this._remoteId;
}

on<EventName extends keyof SocketEventSet>(eventName: EventName, handler: SocketEventSet[EventName]) {
this._ee.on(eventName, handler);
Expand Down
2 changes: 1 addition & 1 deletion lib/UhstHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions lib/WebRTCSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class WebRTCSocket implements UhstSocket {
private _ee = new EventEmitter<SocketEventSet>();
private _pendingCandidates: (RTCIceCandidate | RTCIceCandidateInit)[] = [];
private _offerAccepted = false;
private _remoteId;
private token: string;
private connection: RTCPeerConnection;
private dataChannel: RTCDataChannel;
Expand All @@ -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 extends keyof SocketEventSet>(eventName: EventName, handler: SocketEventSet[EventName]) {
this._ee.on(eventName, handler);
Expand Down
3 changes: 3 additions & 0 deletions lib/contracts/UhstSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type SocketEventSet = {
}

export interface UhstSocket {
readonly remoteId: string;

on<EventName extends keyof SocketEventSet>(eventName: EventName, handler: SocketEventSet[EventName]);

once<EventName extends keyof SocketEventSet>(eventName: EventName, handler: SocketEventSet[EventName]);
Expand All @@ -23,4 +25,5 @@ export interface UhstSocket {
close();

handleMessage(message: Message);

}
1 change: 1 addition & 0 deletions lib/models/SocketParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface HostSocketParams {
type: "host",
token: string,
clientId: string,
sendUrl?: string
}

Expand Down
3 changes: 3 additions & 0 deletions test/RelaySocket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe("# RelaySocket", () => {
const mockHostSocketParams: HostSocketParams = {
type: "host",
token: mockToken,
clientId: "testClient",
sendUrl: "hostSendUrl"
};

Expand All @@ -70,6 +71,7 @@ describe("# RelaySocket", () => {
const mockHostSocketParams: HostSocketParams = {
type: "host",
token: mockToken,
clientId: "testClient",
sendUrl: "hostSendUrl"
};

Expand Down Expand Up @@ -101,6 +103,7 @@ describe("# RelaySocket", () => {
const mockHostSocketParams: HostSocketParams = {
type: "host",
token: mockToken,
clientId: "testClient",
sendUrl: "hostSendUrl"
};

Expand Down
1 change: 1 addition & 0 deletions test/RelaySocketProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("# RelaySocketProvider", () => {
const provider = new RelaySocketProvider();
const mockHostSocketParams: HostSocketParams = {
type: "host",
clientId: "testClient",
token: "responseToken"
};
expect(provider.createUhstSocket(<UhstRelayClient>{}, mockHostSocketParams, false)).to.not.be.null;
Expand Down

0 comments on commit f0c83ac

Please sign in to comment.