mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
harmonize with websocket handling
This commit is contained in:
parent
cacd7fef6e
commit
fc831a8149
2 changed files with 26 additions and 44 deletions
|
@ -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() {
|
||||
|
|
|
@ -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({
|
||||
|
|
Loading…
Add table
Reference in a new issue