2014-11-26 03:18:21 +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-09 17:18:14 +00:00
|
|
|
ConnectionActions.open();
|
|
|
|
};
|
|
|
|
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-09 17:18:14 +00:00
|
|
|
ConnectionActions.error();
|
|
|
|
EventLogActions.add_event("WebSocket connection error.");
|
|
|
|
};
|
2014-11-27 00:40:26 +00:00
|
|
|
ws.onclose = function () {
|
2014-12-09 17:18:14 +00:00
|
|
|
ConnectionActions.close();
|
|
|
|
EventLogActions.add_event("WebSocket connection closed.");
|
|
|
|
};
|
|
|
|
return ws;
|
|
|
|
}
|