diff --git a/changelog.md b/changelog.md index f19f5c2d3..b4343de44 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,13 @@ # PeerServer Changelog +### 0.2.11 + +* Decreased error logging for missing peer event. + +### 0.2.10 + +* Added some more error logging. + ### 0.2.6 * Ensure 16 character IDs. diff --git a/lib/server.js b/lib/server.js index 00480ddf6..a6e8ef063 100644 --- a/lib/server.js +++ b/lib/server.js @@ -20,19 +20,20 @@ app._initializeWSS = function(server) { // Create WebSocket server as well. this._wss = new WebSocketServer({ path: path, server: server}); - this._wss.on('connection', function(socket) { + this._wss.on('connection', function(socket, req) { + socket.upgradeReq = req; var query = url.parse(socket.upgradeReq.url, true).query; var id = query.id; var token = query.token; var key = query.key; var ip = socket.upgradeReq.socket.remoteAddress; - socket.on("error", function(e) { - console.log("Error in WS socket."); - console.log(e); + socket.on('error', function(e) { + self._log('Error in WS socket.', e); }); if (!id || !token || !key) { + self._warn('No id, token, or key supplied to websocket server', id, token, key); socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: 'No id, token, or key supplied to websocket server' } })); socket.close(); return; @@ -48,6 +49,7 @@ app._initializeWSS = function(server) { } self._configureWS(socket, key, id, token); } else { + self._warn('Client key check failure for wss: ' + err, key, ip); socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: err } })); } }); @@ -71,6 +73,7 @@ app._configureWS = function(socket, key, id, token) { } } else { // ID-taken, invalid token + self._warn('Client token already taken: ', key, id, token); socket.send(JSON.stringify({ type: 'ID-TAKEN', payload: { msg: 'ID is taken' } })); socket.close(); return; @@ -101,7 +104,7 @@ app._configureWS = function(socket, key, id, token) { } else if(message.type === 'PING') { // Do nothing } else { - util.prettyError('Message unrecognized'); + self._warn('Message unrecognized:', message && message.type); } } catch(e) { self._log('Invalid message', data); @@ -174,6 +177,7 @@ app._initializeHTTP = function() { self._ips[ip]++; self._startStreaming(res, key, id, token, true); } else { + self._warn('Client key check failure for XHR: ' + err, key, ip, id); res.send(JSON.stringify({ type: 'HTTP-ERROR' })); } }); @@ -205,6 +209,7 @@ app._initializeHTTP = function() { var client; if (!self._clients[key] || !(client = self._clients[key][id])) { if (req.params.retry) { + self._log('Client check failure on retry for: ', key, id); res.sendStatus(401); return; } else { @@ -217,6 +222,7 @@ app._initializeHTTP = function() { // Auth the req if (typeof client === 'undefined' || req.params.token !== client.token) { + self._warn('Client check failure on request for: ', key, id); res.sendStatus(401); return; } else { @@ -274,6 +280,7 @@ app._startStreaming = function(res, key, id, token, open) { client.res = res; this._processOutstanding(key, id); } else { + self._warn('Client token check failure for streaming: ', key, id, token); // ID-taken, invalid token res.end(JSON.stringify({ type: 'HTTP-ERROR' })); } @@ -402,8 +409,21 @@ app._generateClientId = function(key) { return clientId; }; + +// logging methods + +var _logLevels = { + ERROR: 1, WARN: 2, INFO: 3, DEBUG: 4 +} + app._log = function() { - if (this._options.debug) { + if (this._options.debug === true || this._options.debug >= _logLevels.DEBUG) { console.log.apply(console, arguments); } }; + +app._warn = function() { + if (this._options.debug === true || this._options.debug >= _logLevels.WARN ) { + console.warn.apply(console, arguments); + } +}; diff --git a/package.json b/package.json index 0276a9eb2..a54684d6b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "peer", - "version": "0.2.9", + "version": "0.2.11", "description": "PeerJS server component", "main": "lib/index.js", "bin": { @@ -16,7 +16,7 @@ "body-parser": "^1.9.0", "express": "^4.9.8", "optimist": "~0.6.1", - "ws": "*", + "ws": "^3.0.0", "cors": "~2.5.0" }, "devDependencies": {