2014-12-09 17:18:14 +00:00
|
|
|
|
2014-12-30 08:51:07 +00:00
|
|
|
var actions = require("./actions.js");
|
2015-03-22 14:19:35 +00:00
|
|
|
var AppDispatcher = require("./dispatcher.js").AppDispatcher;
|
2014-12-30 08:51:07 +00:00
|
|
|
|
|
|
|
function Connection(url) {
|
2014-12-09 17:18:14 +00:00
|
|
|
if (url[0] === "/") {
|
|
|
|
url = location.origin.replace("http", "ws") + url;
|
2014-11-26 03:18:21 +00:00
|
|
|
}
|
2014-12-09 17:18:14 +00:00
|
|
|
|
|
|
|
var ws = new WebSocket(url);
|
2014-11-27 00:40:26 +00:00
|
|
|
ws.onopen = function () {
|
2014-12-30 08:51:07 +00:00
|
|
|
actions.ConnectionActions.open();
|
2014-12-09 17:18:14 +00:00
|
|
|
};
|
|
|
|
ws.onmessage = function (message) {
|
|
|
|
var m = JSON.parse(message.data);
|
|
|
|
AppDispatcher.dispatchServerAction(m);
|
|
|
|
};
|
2014-11-27 00:40:26 +00:00
|
|
|
ws.onerror = function () {
|
2014-12-30 08:51:07 +00:00
|
|
|
actions.ConnectionActions.error();
|
2015-03-22 14:19:35 +00:00
|
|
|
actions.EventLogActions.add_event("WebSocket connection error.");
|
2014-12-09 17:18:14 +00:00
|
|
|
};
|
2014-11-27 00:40:26 +00:00
|
|
|
ws.onclose = function () {
|
2014-12-30 08:51:07 +00:00
|
|
|
actions.ConnectionActions.close();
|
2015-03-22 14:19:35 +00:00
|
|
|
actions.EventLogActions.add_event("WebSocket connection closed.");
|
2014-12-09 17:18:14 +00:00
|
|
|
};
|
|
|
|
return ws;
|
2014-12-30 08:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Connection;
|