Skip to content

Commit 370070b

Browse files
committed
bugfixes, new web-dev-server dependency, new websockets server 3.0.0+ implementation
1 parent c531055 commit 370070b

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example - Chat
22

3-
[![Latest Stable Version](https://img.shields.io/badge/Stable-v1.2.1-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/example-chat/releases)
3+
[![Latest Stable Version](https://img.shields.io/badge/Stable-v1.3.0-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/example-chat/releases)
44
[![License](https://img.shields.io/badge/Licence-BSD-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/chat-example-pure-js/blob/master/LICENCE.md)
55

66
Chat example with session authentication. Client scripts written with pure Javascript, no framework needed.

chat/css/style.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ form.login-form label{
2727
margin:0 0 10px 0;
2828
color:#fff;
2929
}
30-
form.login-form input[type=text]{
30+
form.login-form input[type=text],
31+
form.login-form input[type=password]{
3132
width:220px;
3233
border-radius:4px;
3334
border:1px solid #c5c5c9;

chat/data/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var WebSocketServer = require('ws').Server,
22
Url = require('url'),
3-
fs = require('fs');
3+
fs = require('fs'),
4+
expressSession = require('express-session');
45

56
var App = function (httpServer, expressServer, sessionParser, request, response) {
67
this._init(httpServer, expressServer, sessionParser, request, response);
@@ -32,6 +33,7 @@ App.prototype = {
3233
);
3334
},
3435
_webSocketSessionParsedHandler: function (ws, req) {
36+
ws.upgradeReq = req; // necessary for WebSocketServer 3.0.0+, https://github.com/websockets/ws/pull/1099
3537
var sessionId = req.session.id;
3638
if (typeof(this._allowedSessionIds[sessionId]) == 'undefined') {
3739
console.log("Connected not authorized user with session id: " + sessionId);
@@ -87,7 +89,9 @@ App.prototype = {
8789

8890
} else if (eventName == 'message') {
8991

90-
var recepient = typeof(data.recepient) != 'undefined' ? data.recepient : 'all';
92+
var recepient = typeof(data.recepient) != 'undefined' && data.recepient
93+
? data.recepient
94+
: 'all';
9195

9296
if (recepient == 'all') {
9397
this._sendToAll('message', data);
@@ -116,9 +120,8 @@ App.prototype = {
116120
delete this._onlineUsers[uidToDelete];
117121

118122
var onlineUsersToSendBack = {};
119-
for (var uid in this._onlineUsers) {
123+
for (var uid in this._onlineUsers)
120124
onlineUsersToSendBack[uid] = this._onlineUsers[uid].user;
121-
}
122125

123126
this._sendToAll('logout', {
124127
onlineUsers: onlineUsersToSendBack,
@@ -148,9 +151,8 @@ App.prototype = {
148151
response.targetSessionId = targetSessionId;
149152
this._data.push(response);
150153
this._wss.clients.forEach(function (client) {
151-
if (client.upgradeReq.sessionID == targetSessionId) {
154+
if (client.upgradeReq.sessionID == targetSessionId)
152155
client.send(responseStr);
153-
}
154156
});
155157
},
156158
_sendToMyself: function (eventName, data, ws) {
@@ -192,13 +194,15 @@ App.prototype = {
192194
/***************************************************************************/
193195
if (this._users[data.user] && this._users[data.user].pass == data.pass) {
194196
// 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;
196199

197200
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));
199205

200-
response.send('{"success":true,"id":' + this._users[data.user].id + '}');
201-
callback();
202206
} else {
203207
response.send('{"success":false');
204208
callback();

chat/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<form id="login-form" class="login-form">
1414
<label for="login-user">Please enter chat nick name</label>
1515
<input name="user" type="text" placeholder="Nickname" />
16-
<input name="pass" type="text" placeholder="Password" />
16+
<input name="pass" type="password" placeholder="Password" />
1717
<input type="submit" value="Login" />
1818
</form>
1919

@@ -45,10 +45,10 @@
4545
<label for="all">Send to all</label>
4646
</div>
4747
<div id="recepients">
48-
<div ng-show="recepient.userid != userid">
48+
<!--div>
4949
<input id="id" type="radio" name="rcp" value="id" />
50-
<label for="id">usern</label>
51-
</div>
50+
<label for="id">user</label>
51+
</div-->
5252
</div>
5353
</div>
5454
<button type="submit">Send</button>

chat/js/client.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Class.Define('Chat', {
22
Static: {
3-
ADDRESS: 'ws://localhost:8000/chat-pure-js/data/'
3+
ADDRESS: 'ws://%location.host%%location.pathname%data/'
44
},
55
Constructor: function () {
66
this._initElements();
@@ -40,6 +40,10 @@ Class.Define('Chat', {
4040
return scope._messageFormSubmitHandler(e || window.event);
4141
}
4242
};
43+
window.addEventListener("unload", function(e) {
44+
if (this._socket)
45+
this._socket.close();
46+
}.bind(this));
4347
if (this._development) return;
4448
window.addEventListener("beforeunload", function(e) {
4549
return e.returnValue = "Do you realy want to leave chat?";
@@ -104,7 +108,11 @@ Class.Define('Chat', {
104108
_initChatWebSocketComunication: function () {
105109
var scope = this;
106110
// connect to server:
107-
this._socket = SocketWrapper.getInstance(this.self.ADDRESS);
111+
this._socket = SocketWrapper.getInstance(
112+
this.self.ADDRESS
113+
.replace('%location.host%', location.host)
114+
.replace('%location.pathname%', location.pathname)
115+
);
108116
// tell the server to login this user:
109117
this._socket.send('login', {
110118
id: this._id,
@@ -186,6 +194,7 @@ Class.Define('Chat', {
186194
},
187195
_updateRecepients: function (onlineUsers) {
188196
var html = '';
197+
console.log(onlineUsers);
189198
for (var id in onlineUsers) {
190199
if (id == this._id) continue;
191200
html += '<div>'

chat/js/socket-wrapper.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SocketWrapper.prototype = {
1717
_callbacks: {},
1818
send: function (eventName, data) {
1919
var str = JSON.stringify({eventName: eventName, data: data});
20+
console.log(this._opened, str);
2021
if (this._opened) {
2122
this._socket.send(str);
2223
} else {
@@ -51,7 +52,7 @@ SocketWrapper.prototype = {
5152
};
5253
return this;
5354
},
54-
_init: function (username) {
55+
_init: function () {
5556
if (this._connect()) {
5657
this._socket.addEventListener('open', this._onOpenHandler.bind(this));
5758
this._socket.addEventListener('error', this._onErrorHandler.bind(this));
@@ -70,17 +71,21 @@ SocketWrapper.prototype = {
7071
return r;
7172
},
7273
_onOpenHandler: function (event) {
73-
var eventName = 'open', callbacks = [];
74-
this._opened = true;
75-
if (typeof(this._callbacks[eventName]) != 'undefined') {
76-
this._processCallbacks(this._callbacks[eventName], [event]);
77-
}
78-
if (this._sendQueue.length) {
79-
for (var i = 0, l = this._sendQueue.length; i < l; i++) {
80-
this._socket.send(this._sendQueue[i]);
81-
}
82-
this._sendQueue = [];
83-
}
74+
var eventName = 'open',
75+
callbacks = [];
76+
try {
77+
this._opened = true;
78+
if (typeof(this._callbacks[eventName]) != 'undefined') {
79+
this._processCallbacks(this._callbacks[eventName], [event]);
80+
}
81+
if (this._sendQueue.length) {
82+
for (var i = 0, l = this._sendQueue.length; i < l; i++)
83+
this._socket.send(this._sendQueue[i]);
84+
this._sendQueue = [];
85+
}
86+
} catch (e) {
87+
console.error(e);
88+
}
8489
},
8590
_onErrorHandler: function (event) {
8691
var eventName = 'error', callbacks = [], intId = 0;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-dev-server-example-chat-pure-js",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Chat example with session authentication in pure Javascript.",
55
"homepage": "https://github.com/web-dev-server/example-chat-pure-js",
66
"repository": {
@@ -15,7 +15,7 @@
1515
},
1616
"main": "server.js",
1717
"dependencies": {
18-
"web-dev-server": "^1.2.0",
18+
"web-dev-server": "^1.3.0",
1919
"ws": "^3.3.2"
2020
}
2121
}

0 commit comments

Comments
 (0)