-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
167 lines (120 loc) · 4.5 KB
/
server.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
164
"use strict";
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-notification';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
/**
* Global variables
*/
// latest 100 messages
var history = [];
// list of currently connected clients (users)
//var clients = [];
var clients = {};
/**
* Helper function for escaping input strings
*/
function htmlEntities(str) {
return String(str)
.replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}
/**
* HTTP server
*/
var server = http.createServer(function (request, response) {
// Not important for us. We're writing WebSocket server,
// not HTTP server
});
server.listen(webSocketsServerPort, function () {
console.log((new Date()) + " Server is listening on port "
+ webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket
// request is just an enhanced HTTP request. For more info
// http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function (request) {
console.log((new Date()) + ' Connection from origin '
+ request.origin + '.');
// accept connection - you should check 'request.origin' to
// make sure that client is connecting from your website
// (http://en.wikipedia.org/wiki/Same_origin_policy)
var connection = request.accept(null, request.origin);
// we need to know client index to remove them on 'close' event
// var index = clients.push(connection) - 1;
var userName = false;
console.log((new Date()) + ' Connection accepted.');
// user sent some message -- msg from client --
connection.on('message', function (message) {
if (message.type === 'utf8') {
if (userName === false) {
// remember user name
userName = htmlEntities(message.utf8Data);
console.log((new Date()) + ' User is known as: ' + userName);
//store connection infos in object
if (userName in clients) {
delete clients[userName];
clients[userName] = connection;
} else {
clients[userName] = connection;
}
} else {
if (JSON.parse(message.utf8Data).type === 'chatMsg') {
// log and broadcast the message
console.log((new Date()) + ' Received Message from '
+ userName + ': ' + message.utf8Data);
// we want to keep history of all sent messages
var obj = {
time: (new Date()).getTime(),
text: htmlEntities(message.utf8Data),
author: userName,
};
history.push(obj);
history = history.slice(-100);
// broadcast message to all connected clients
var json = JSON.stringify({type: 'message', data: obj});
for (var key in clients) {
clients[key].sendUTF(json);
}
} else {
json = JSON.parse(message.utf8Data);
if (json.type == 'ask') {
var jsonToSend = JSON.stringify({type: 'notifyRequest', data: userName});
var owner = json.owner;
clients[owner].sendUTF(jsonToSend);
}
if (json.type == 'accepted') {
var jsonToSend = JSON.stringify({type: 'notifyAccepted', data: userName});
var requester = json.requester;
clients[requester].sendUTF(jsonToSend);
}
}
}
}
});
// user disconnected
connection.on('close', function (connection) {
if (userName !== false) {
delete clients[userName];
var size = Object.size(clients);
}
});
});
Object.size = function (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};