|
1 | 1 | var WebSocketServer = require('ws').Server, |
2 | 2 | Url = require('url'), |
3 | | - fs = require('fs'); |
| 3 | + fs = require('fs'), |
| 4 | + expressSession = require('express-session'); |
4 | 5 |
|
5 | 6 | var App = function (httpServer, expressServer, sessionParser, request, response) { |
6 | 7 | this._init(httpServer, expressServer, sessionParser, request, response); |
@@ -32,6 +33,7 @@ App.prototype = { |
32 | 33 | ); |
33 | 34 | }, |
34 | 35 | _webSocketSessionParsedHandler: function (ws, req) { |
| 36 | + ws.upgradeReq = req; // necessary for WebSocketServer 3.0.0+, https://github.com/websockets/ws/pull/1099 |
35 | 37 | var sessionId = req.session.id; |
36 | 38 | if (typeof(this._allowedSessionIds[sessionId]) == 'undefined') { |
37 | 39 | console.log("Connected not authorized user with session id: " + sessionId); |
@@ -87,7 +89,9 @@ App.prototype = { |
87 | 89 |
|
88 | 90 | } else if (eventName == 'message') { |
89 | 91 |
|
90 | | - var recepient = typeof(data.recepient) != 'undefined' ? data.recepient : 'all'; |
| 92 | + var recepient = typeof(data.recepient) != 'undefined' && data.recepient |
| 93 | + ? data.recepient |
| 94 | + : 'all'; |
91 | 95 |
|
92 | 96 | if (recepient == 'all') { |
93 | 97 | this._sendToAll('message', data); |
@@ -116,9 +120,8 @@ App.prototype = { |
116 | 120 | delete this._onlineUsers[uidToDelete]; |
117 | 121 |
|
118 | 122 | var onlineUsersToSendBack = {}; |
119 | | - for (var uid in this._onlineUsers) { |
| 123 | + for (var uid in this._onlineUsers) |
120 | 124 | onlineUsersToSendBack[uid] = this._onlineUsers[uid].user; |
121 | | - } |
122 | 125 |
|
123 | 126 | this._sendToAll('logout', { |
124 | 127 | onlineUsers: onlineUsersToSendBack, |
@@ -148,9 +151,8 @@ App.prototype = { |
148 | 151 | response.targetSessionId = targetSessionId; |
149 | 152 | this._data.push(response); |
150 | 153 | this._wss.clients.forEach(function (client) { |
151 | | - if (client.upgradeReq.sessionID == targetSessionId) { |
| 154 | + if (client.upgradeReq.sessionID == targetSessionId) |
152 | 155 | client.send(responseStr); |
153 | | - } |
154 | 156 | }); |
155 | 157 | }, |
156 | 158 | _sendToMyself: function (eventName, data, ws) { |
@@ -192,13 +194,15 @@ App.prototype = { |
192 | 194 | /***************************************************************************/ |
193 | 195 | if (this._users[data.user] && this._users[data.user].pass == data.pass) { |
194 | 196 | // after session is authorized - set session id authorization boolean to true: |
195 | | - this._allowedSessionIds[request.session.id] = true; |
| 197 | + var sessionId = request.session.id; |
| 198 | + this._allowedSessionIds[sessionId] = true; |
196 | 199 |
|
197 | 200 | request.session.authorized = true; |
198 | | - request.session.save(); |
| 201 | + request.session.save(function () { |
| 202 | + response.send('{"success":true,"id":' + this._users[data.user].id + '}'); |
| 203 | + callback(); |
| 204 | + }.bind(this)); |
199 | 205 |
|
200 | | - response.send('{"success":true,"id":' + this._users[data.user].id + '}'); |
201 | | - callback(); |
202 | 206 | } else { |
203 | 207 | response.send('{"success":false'); |
204 | 208 | callback(); |
|
0 commit comments