Skip to content

Commit

Permalink
[jsonrpc] Support Notification Handlers (#204)
Browse files Browse the repository at this point in the history
* [jsonrpc] support notification handler registration

* fix lint
  • Loading branch information
billylindeman authored May 27, 2021
1 parent 3ead90f commit dbacb5a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/signal/json-rpc-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class IonSFUJSONRPCSignal implements Signal {
onnegotiate?: (jsep: RTCSessionDescriptionInit) => void;
ontrickle?: (trickle: Trickle) => void;

private _notifyhandlers: { [ method: string ]: (params: any) => void };

constructor(uri: string) {
this.socket = new WebSocket(uri);
this._notifyhandlers = {};

this.socket.addEventListener('open', () => {
if (this._onopen) this._onopen();
Expand All @@ -31,10 +34,21 @@ class IonSFUJSONRPCSignal implements Signal {
if (this.onnegotiate) this.onnegotiate(resp.params);
} else if (resp.method === 'trickle') {
if (this.ontrickle) this.ontrickle(resp.params);
} else {
const handler = this._notifyhandlers[resp.method];
if (handler) {
handler(resp.params);
}
}

});
}


on_notify<T>(method: string, cb: (params: T) => void) {
this._notifyhandlers[method] = cb
}

// JsonRPC2 Call
async call<T>(method: string, params: any): Promise<T> {
const id = uuidv4();
Expand Down

0 comments on commit dbacb5a

Please sign in to comment.