2017-01-21 17:06:19 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-02-07 21:49:55 +00:00
|
|
|
import { WebSocketStates, ReverseWebSocketStates } from './Utilities/WebSocketStates';
|
2017-01-21 17:06:19 +00:00
|
|
|
|
2017-02-04 21:26:48 +00:00
|
|
|
const EventEmitter = require("event-emitter-es6");
|
|
|
|
|
|
|
|
class RippleLink extends EventEmitter
|
2017-01-21 17:06:19 +00:00
|
|
|
{
|
2017-01-21 18:38:52 +00:00
|
|
|
constructor(inSocketUrl, inBoardWindow)
|
2017-01-21 17:06:19 +00:00
|
|
|
{
|
2017-02-04 21:26:48 +00:00
|
|
|
super();
|
|
|
|
|
2017-01-21 18:38:52 +00:00
|
|
|
this.socketUrl = inSocketUrl;
|
|
|
|
this.boardWindow = inBoardWindow;
|
|
|
|
this.settings = this.boardWindow.settings;
|
2017-01-21 17:06:19 +00:00
|
|
|
|
2017-02-04 21:26:48 +00:00
|
|
|
// Create the websocket and commect to the server
|
2017-06-27 09:57:50 +00:00
|
|
|
|
|
|
|
// Whether the link ot the nibri server is open or not
|
|
|
|
this.linkOpen = false;
|
|
|
|
// The underlying websocket
|
2017-02-04 21:26:48 +00:00
|
|
|
this.websocket = new WebSocket(this.socketUrl, [ this.settings.WebsocketProtocol ]);
|
2017-06-27 09:57:50 +00:00
|
|
|
// Attach some event listeners
|
2017-02-04 21:26:48 +00:00
|
|
|
this.websocket.addEventListener("open", this.handleConnection.bind(this));
|
|
|
|
this.websocket.addEventListener("message", this.handleMessage.bind(this));
|
|
|
|
this.websocket.addEventListener("close", this.handleDisconnection.bind(this));
|
|
|
|
|
2017-02-19 16:35:12 +00:00
|
|
|
// Respond to heartbeats from the server
|
|
|
|
this.on("Heartbeat", this.handleHeartbeat.bind(this));
|
2017-04-23 16:26:29 +00:00
|
|
|
this.on("Error", this.handleErrorMessage.bind(this));
|
2017-02-19 16:35:12 +00:00
|
|
|
|
2017-02-05 14:46:40 +00:00
|
|
|
// Close the socket correctly
|
|
|
|
window.addEventListener("beforeunload", (function(event) {
|
|
|
|
this.websocket.close();
|
|
|
|
}).bind(this));
|
2017-02-04 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleConnection(event) {
|
|
|
|
console.info("[ripple link] Established connection successfully.");
|
2017-06-27 09:57:50 +00:00
|
|
|
this.linkOpen = true;
|
2017-02-04 21:26:48 +00:00
|
|
|
// Tell everyone about it
|
|
|
|
this.emit("connect", event);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDisconnection(event) {
|
2017-06-27 09:57:50 +00:00
|
|
|
// If the link was already down, then ignore this phantom disconnection
|
|
|
|
if(!this.linkOpen)
|
|
|
|
return;
|
2017-02-04 21:26:48 +00:00
|
|
|
console.error("[ripple link] Lost connection.");
|
2017-04-28 17:22:09 +00:00
|
|
|
this.boardWindow.interface.setConnectedStatus(false);
|
2017-04-25 18:34:03 +00:00
|
|
|
|
2017-02-04 21:26:48 +00:00
|
|
|
this.emit("disconnect", event);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(event) {
|
|
|
|
// Decode the message form the server
|
|
|
|
var message = JSON.parse(event.data);
|
2017-02-19 13:22:35 +00:00
|
|
|
console.debug(message.Event, message);
|
2017-02-04 21:26:48 +00:00
|
|
|
|
|
|
|
// Pass it on to the board manager by triggering the appropriate event
|
2017-02-19 13:22:35 +00:00
|
|
|
this.emit(message.Event, message);
|
2017-02-04 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 16:26:29 +00:00
|
|
|
handleErrorMessage(message) {
|
|
|
|
console.error(message.Message);
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:35:12 +00:00
|
|
|
/**
|
|
|
|
* Replies to heartbeat messages from the server.
|
|
|
|
*/
|
|
|
|
handleHeartbeat(message) {
|
|
|
|
// Reply with a heartbeat
|
|
|
|
this.send({
|
|
|
|
"Event": "Heartbeat"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-04 21:26:48 +00:00
|
|
|
/**
|
|
|
|
* Sends a message object to the server.
|
|
|
|
*/
|
|
|
|
send(message) {
|
|
|
|
if(this.websocket.readyState !== WebSocketStates.ready)
|
|
|
|
{
|
2017-02-07 21:49:55 +00:00
|
|
|
console.error(`Attempt to send data on the RippleLink when it is not ready (state ${ReverseWebSocketStates[this.websocket.readyState]})`);
|
2017-02-04 21:26:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.websocket.send(JSON.stringify(message));
|
|
|
|
return true;
|
2017-01-21 17:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RippleLink;
|