-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_full.js
70 lines (42 loc) · 1.39 KB
/
ws_full.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
"use strict"
process.title ='node-chat';
var webSocketsServerPort = 1337;
var webSocketServer = require('websocket').server;
var http = require('http');
var history = [];
var clients = [];
function htmlEntities(str)
{
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"e;');
}
var colors =['red','green','blue', 'magenta', 'purple', 'plum','orange'];
colors.sort(function(a,b){return Math.random() > 0.5; });
//HTTP server
var server = http.createServer(function(request, response){
});
server.listen(webSocketsServerPort, function(){
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
//webSocketServer
var wsServer = new webSocketServer({
httpServer: server
});
//This function gets called when someone tries to connect to the web socket server
wsServer.on('request', function(request){
console.log((Date()) + ' Connection from origin ' + request.origin + '.');
var connection = request.accept(null, request.origin);
var index = clients.push(connection) - 1;
var userName = false;
var userColor = false;
console.log((new Date())+ ' Connection accepted.');
if(history.length > 0){
connection.sendUTF(JSON.stringsify({type: 'history', data: history} ));
}
connection.on('message', function(message){
if(message.type === 'utf8'){
if(userName === false){
}
}
});
});