namibia
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
namibia / public / js / app / websocket.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
/** * Websocket protocol handler. */ ; (function () { window._ = {}; _.Api = {}; _.Api.websocket = { /** * URI of the websocket server. * @type {String} */ _serverUri: null, /** * The socket. * @type {Object} */ _socket: null, /** * State of things. * @type {String} */ _state: 'closed', /** * Latency function to estimate latency and keep connection alive. * @type {Function} */ _latencyChecker: null, /** * Latency check start time. * @type {Date} */ _pingStart: null, /** * Latency. * @type {Array} */ _latencies: [], /** * Latency. * @type {Integer} */ _latencyAverage: 0, /** * Message processor callback. * @type {Function} */ _messageCallback: function(){}, /** * Connection opened callback. * @type {Function} */ _connectionOpenedCallback: function(){}, /** * Connection closed callback. * @type {Function} */ _connectionClosedCallback: function(){}, /** * Initialize instance. * @return {} */ _initialize: function () { //-- Establish server uri and connect. bug.info('Websocket', '_initialize', 'Initializing websocket submodule.'); //_.Api.websocket._serverUri = 'ws://' + window.location.hostname + ':' + window.location.port; _.Api.websocket._serverUri = 'ws://' + window.location.hostname; bug.dump('Websocket', '_initialize', 'Server URI: ' + _.Api.websocket._serverUri); _.Api.websocket._state = 'open'; _.Api.websocket.connect(); }, /** * Connect to websocket server. */ connect: function () { //-- Create a new websocket instance. _.Api.websocket._socket = new WebSocket(_.Api.websocket._serverUri); //-- Handle open event. _.Api.websocket._socket.onopen = function (ev) { _.Api.websocket._state = 'open'; _.Api.websocket._latencyChecker = setInterval(function () { if (WebSocket.OPEN == _.Api.websocket._socket.readyState) { _.Api.websocket._pingStart = new Date(); _.Api.websocket._socket.send('{"ping":true}'); } }, 540000); _.Api.websocket._connectionOpenedCallback(); bug.info('Websocket', '_connect', 'Connection established with ' + _.Api.websocket._serverUri + '.'); } //-- Handle message event. _.Api.websocket._socket.onmessage = function (ev) { var message = JSON.parse(ev.data); bug.info('Websocket', 'received', message); if (message.pong) { var _pingEnd = new Date(); _.Api.websocket._latencies.push((_pingEnd.getTime() - _.Api.websocket._pingStart.getTime()) / 2); if (10 < _.Api.websocket._latencies.length) { _.Api.websocket._latencies.shift(); } var sum = _.Api.websocket._latencies.reduce(function (a, b) { return a + b; }); _.Api.websocket._latencyAverage = sum / _.Api.websocket._latencies.length; bug.info('Websocket', 'latency', _.Api.websocket._latencyAverage); } else { _.Api.websocket._messageCallback(message); } }; //-- Handle error event. _.Api.websocket._socket.onerror = function (ev) { bug.warn('Websocket', '_connect', ev); }; //-- Handle close event. _.Api.websocket._socket.onclose = function (ev) { _.Api.websocket._socket = null; clearInterval(_.Api.websocket._latencyChecker); _.Api.websocket._connectionClosedCallback(); if ('closed' == _.Api.websocket._state) { return; } bug.info('Websocket', '_connect.onclose', 'Connection closed.'); bug.dump('Websocket', '_connect.onclose', ev); setTimeout(function () { $.proxy(_.Api.websocket.connect, _.Api.websocket)() }, 15000 ); }; }, close: function () { _.Api.websocket._state = 'closed'; _.Api.websocket._socket.close(); } }; })(); |
Commits for namibiapublic/js/app/websocket.js
Revision | Author | Commited | Message |
---|---|---|---|
df0489 ... | Mark | Fri 14 Oct, 2016 10:01:00 +0000 | initial commit |