1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/web/ synced 2025-03-09 00:00:01 +01:00

websocket: handle reconnects

This commit is contained in:
Steffen Vogel 2018-06-04 20:30:15 +02:00
parent 5dcbb09854
commit 302dac284e
2 changed files with 53 additions and 14 deletions

View file

@ -20,21 +20,60 @@
******************************************************************************/
class WebsocketAPI {
addSocket(endpoint, callbacks) {
// create web socket client
const socket = new WebSocket(this.getURL(endpoint), 'live');
socket.binaryType = 'arraybuffer';
constructor(endpoint, callbacks) {
this.endpoint = endpoint;
this.callbacks = callbacks;
// register callbacks
if (callbacks.onOpen) socket.onopen = callbacks.onOpen;
if (callbacks.onClose) socket.onclose = callbacks.onClose;
if (callbacks.onMessage) socket.onmessage = callbacks.onMessage;
if (callbacks.onError) socket.onerror = callbacks.onError;
this.isClosing = false;
return socket;
this.connect(endpoint, callbacks);
}
getURL(endpoint) {
connect(endpoint, callbacks) {
// create web socket client
this.socket = new WebSocket(WebsocketAPI.getURL(endpoint), 'live');
this.socket.binaryType = 'arraybuffer';
this.socket.onclose = this.onClose;
// register callbacks
if (callbacks.onOpen)
this.socket.onopen = callbacks.onOpen;
if (callbacks.onMessage)
this.socket.onmessage = callbacks.onMessage;
if (callbacks.onError)
this.socket.onerror = callbacks.onError;
}
reconnect() {
//console.log("Reconnecting: " + this.endpoint);
this.connect(this.endpoint, this.callbacks);
}
get url() {
return WebsocketAPI.getURL(this.endpoint);
}
send(data) {
this.socket.send(data);
}
close(code, reason) {
this.isClosing = true;
this.socket.close(code, reason);
}
onClose = e => {
if (this.isClosing) {
if (this.callbacks.onClose)
this.callbacks.onClose(e);
}
else {
//console.log("Connection to " + this.endpoint + " dropped. Attempt reconnect in 1 sec");
window.setTimeout(() => { this.reconnect(); }, 500);
}
}
static getURL(endpoint) {
// create an anchor element (note: no need to append this element to the document)
var link = document.createElement('a');
link.href = endpoint;
@ -48,4 +87,4 @@ class WebsocketAPI {
}
}
export default new WebsocketAPI();
export default WebsocketAPI;

View file

@ -37,10 +37,10 @@ class SimulatorDataDataManager {
// replace connection, since endpoint changed
this._sockets.close();
this._sockets[identifier] = WebsocketAPI.addSocket(endpoint, { onOpen: (event) => this.onOpen(event, identifier), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier), onError: (error) => this.onError(error, identifier) });
this._sockets[identifier] = new WebsocketAPI(endpoint, { onOpen: (event) => this.onOpen(event, identifier), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier), onError: (error) => this.onError(error, identifier) });
}
} else {
this._sockets[identifier] = WebsocketAPI.addSocket(endpoint, { onOpen: (event) => this.onOpen(event, identifier, false), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier), onError: (error) => this.onError(error, identifier) });
this._sockets[identifier] = new WebsocketAPI(endpoint, { onOpen: (event) => this.onOpen(event, identifier, false), onClose: (event) => this.onClose(event, identifier), onMessage: (event) => this.onMessage(event, identifier), onError: (error) => this.onError(error, identifier) });
}
}