mitmproxy/web/src/js/connection.js

29 lines
827 B
JavaScript
Raw Normal View History

2014-12-09 17:18:14 +00:00
var actions = require("./actions.js");
2015-03-22 14:19:35 +00:00
var AppDispatcher = require("./dispatcher.js").AppDispatcher;
function Connection(url) {
2014-12-09 17:18:14 +00:00
if (url[0] === "/") {
url = location.origin.replace("http", "ws") + url;
}
2014-12-09 17:18:14 +00:00
var ws = new WebSocket(url);
2014-11-27 00:40:26 +00:00
ws.onopen = function () {
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 () {
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 () {
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;
}
module.exports = Connection;