mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 16:17:49 +00:00
29 lines
827 B
JavaScript
29 lines
827 B
JavaScript
|
|
var actions = require("./actions.js");
|
|
var AppDispatcher = require("./dispatcher.js").AppDispatcher;
|
|
|
|
function Connection(url) {
|
|
if (url[0] === "/") {
|
|
url = location.origin.replace("http", "ws") + url;
|
|
}
|
|
|
|
var ws = new WebSocket(url);
|
|
ws.onopen = function () {
|
|
actions.ConnectionActions.open();
|
|
};
|
|
ws.onmessage = function (message) {
|
|
var m = JSON.parse(message.data);
|
|
AppDispatcher.dispatchServerAction(m);
|
|
};
|
|
ws.onerror = function () {
|
|
actions.ConnectionActions.error();
|
|
actions.EventLogActions.add_event("WebSocket connection error.");
|
|
};
|
|
ws.onclose = function () {
|
|
actions.ConnectionActions.close();
|
|
actions.EventLogActions.add_event("WebSocket connection closed.");
|
|
};
|
|
return ws;
|
|
}
|
|
|
|
module.exports = Connection; |