diff options
Diffstat (limited to 'web/src/js/connection.js')
-rw-r--r-- | web/src/js/connection.js | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/web/src/js/connection.js b/web/src/js/connection.js index 6177938e..71d20f46 100644 --- a/web/src/js/connection.js +++ b/web/src/js/connection.js @@ -1,19 +1,22 @@ import {ConnectionActions, EventLogActions} from "./actions.js"; import {AppDispatcher} from "./dispatcher.js"; +import * as websocketActions from "./ducks/websocket" -function Connection(url) { +export default function Connection(url, dispatch) { if (url[0] === "/") { url = location.origin.replace("http", "ws") + url; } var ws = new WebSocket(url); ws.onopen = function () { + dispatch(websocketActions.connected()); ConnectionActions.open(); }; - ws.onmessage = function (message) { - var m = JSON.parse(message.data); - AppDispatcher.dispatchServerAction(m); + ws.onmessage = function (m) { + var message = JSON.parse(m.data); + AppDispatcher.dispatchServerAction(message); + dispatch(websocketActions.receiveMessage(message)); }; ws.onerror = function () { ConnectionActions.error(); @@ -22,8 +25,7 @@ function Connection(url) { ws.onclose = function () { ConnectionActions.close(); EventLogActions.add_event("WebSocket connection closed."); + dispatch(websocketActions.disconnected()); }; return ws; -} - -export default Connection;
\ No newline at end of file +}
\ No newline at end of file |