From fc831a8149ed809ae138327552674d1142f8c7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iris=20Marie=20K=C3=B6ster?= Date: Fri, 22 Apr 2022 17:03:26 +0200 Subject: [PATCH] harmonize with websocket handling --- src/common/api/webrtc.js | 48 ++++++++-------------------------- src/ic/ic-data-data-manager.js | 22 +++++++++++----- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/src/common/api/webrtc.js b/src/common/api/webrtc.js index 341ab28..1f08629 100644 --- a/src/common/api/webrtc.js +++ b/src/common/api/webrtc.js @@ -20,7 +20,7 @@ const OFFSET_TYPE = 2; const OFFSET_VERSION = 4; class WebRTC { - constructor(sessionurl, identifier) { + constructor(sessionurl, identifier, callbacks) { this.identifier = identifier this.first = false; this.polite = false; @@ -39,13 +39,14 @@ class WebRTC { 'turn:turn.0l.de:3478?transport=tcp' ]; - this.connectPeers(sessionurl) + console.log(callbacks) + this.onOpen = callbacks.onOpen.bind(this); + this.onMessage = callbacks.onMessage.bind(this); + this.onClose = callbacks.onClose.bind(this); + this.connectPeers(sessionurl, callbacks); } - // Connect the two peers. Normally you look for and connect to a remote - // machine here, but we're just connecting two local objects, so we can - // bypass that step. - connectPeers(sessionurl) { + connectPeers(sessionurl, callbacks) { // Create the local connection and its event listeners this.peerConnection = new RTCPeerConnection({ iceServers: [{ @@ -184,39 +185,12 @@ class WebRTC { console.error(err); } } - + // Handle onmessage events for the receiving channel. // These are the data messages sent by the sending channel. - async handleDataChannelMessage(event) { - let data = new DataView(await event.data.arrayBuffer()) - - if (data.byteLength === 0) { - return null; - } - - const source_index = data.getUint8(1); - const bits = data.getUint8(0); - const length = data.getUint16(0x02, 1); - const bytes = length * 4 + 16; - - let msgarr = { - version: (bits >> OFFSET_VERSION) & 0xF, - type: (bits >> OFFSET_TYPE) & 0x3, - source_index: source_index, - length: length, - sequence: data.getUint32(0x04, 1), - timestamp: data.getUint32(0x08, 1) * 1e3 + data.getUint32(0x0C, 1) * 1e-6, - values: new Float32Array(data.buffer, data.byteOffset + 0x10, length), - blob: new DataView(data.buffer, data.byteOffset + 0x00, bytes), - }; - - if (msgarr) { - AppDispatcher.dispatch({ - type: 'icData/data-changed', - data: [msgarr], - id: this.identifier - }); - } + async handleDataChannelMessage(event) { + let data = await event.data.arrayBuffer() + this.onMessage(data, this.identifier) } disconnectPeers() { diff --git a/src/ic/ic-data-data-manager.js b/src/ic/ic-data-data-manager.js index 31f152d..785d6a6 100644 --- a/src/ic/ic-data-data-manager.js +++ b/src/ic/ic-data-data-manager.js @@ -26,7 +26,7 @@ const OFFSET_VERSION = 4; class IcDataDataManager { constructor() { this._sockets = {}; - this._webrtc = null; + this._webrtc_connections = {}; } open(websocketurl, identifier) { @@ -34,11 +34,14 @@ class IcDataDataManager { if (this._sockets[identifier] != null) return; // already open? - this._sockets[identifier] = new WebsocketAPI(websocketurl, { onOpen: (event) => this.onOpen(event, identifier, true), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier) }); + this._sockets[identifier] = new WebsocketAPI(websocketurl, { onOpen: (event) => this.onOpen(event, identifier, true), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event.data, identifier) }); } openWebRTC(sessionurl, identifier) { - this._webrtc = new WebRTC(sessionurl, identifier) + if (this._webrtc_connections[identifier] != null) + return; // already connected + + this._webrtc_connections[identifier] = new WebRTC(sessionurl, identifier, { onOpen: (event) => this.onOpen(event, identifier, true), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier) }); } update(websocketurl, identifier) { @@ -59,8 +62,12 @@ class IcDataDataManager { } } - if (this._webrtc) { - this._webrtc.disconnectPeers(); + // close all open WebRTC connections + for (var rtc_id in this._webrtc_connections) { + if (this._webrtc_connections.hasOwnProperty(rtc_id)) { + this._webrtc_connections[rtc_id].disconnectPeers(); + delete this._webrtc_connections[rtc_id]; + } } } @@ -95,8 +102,9 @@ class IcDataDataManager { delete this._sockets[identifier]; } - onMessage(event, identifier) { - var msgs = this.bufferToMessageArray(event.data); + onMessage(dataBuffer, identifier) { + console.log(dataBuffer) + var msgs = this.bufferToMessageArray(dataBuffer); if (msgs.length > 0) { AppDispatcher.dispatch({