forked from pallavtrivedi03/StocksAppBackend-WebSocketDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
executable file
·139 lines (110 loc) · 4.63 KB
/
Copy pathServer.js
File metadata and controls
executable file
·139 lines (110 loc) · 4.63 KB
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
"use strict";
// To log the process (ps command)
process.title = 'node-chat';
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
var fs = require("fs");
var history = [ ]; // latest 100 messages
var clients = [ ]; //currently connected clients
var http_files = {};
[
["/jquery.min.js","application/javascript"],
["/Stock-WebPortal.js","application/javascript"],
["/frontend.html","text/html"],
["/backend.html","text/html"]
].forEach(function(fn){
http_files[fn[0]]={
content : fs.readFileSync('.'+fn[0]).toString(),
contentType : fn[1]
};
});
http_files["/"]=http_files["/frontend.html"];
http_files["/index.html"]=http_files["/frontend.html"];
http_files["/backend.html"]=http_files["/backend.html"];
http_files["/backend"]=http_files["/backend.html"];
function handleHtmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}
//Http server
var server = http.createServer(function(request, response) {
// this doubles as a way to serve the fies, and a connection for websocket to use
var file = http_files[request.url];
if (file) {
response.writeHeader(200,{"content-type" : file.contentType});
response.write(file.content);
return response.end();
}
response.writeHeader(404,{"content-type" : "text/plain"});
response.write("not found");
return response.end();
});
server.listen((process.env.PORT || webSocketServer), function() {
console.log((new Date()) + " Server is listening on port " + process.env.PORT);
});
//WebSocket server
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
//when new client tries to connect
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
// We must check 'request.origin' to make sure that
// client is connecting from our website.
//Alternatively, we may use JWT auth later
// (http://en.wikipedia.org/wiki/Same_origin_policy)
var connection = request.accept(null, request.origin);
var index = clients.push(connection) - 1; //clientIndex, to remove it on close
var enteredPassword = false;
let SYSTEM_PASSWORD = "admin";
console.log((new Date()) + ' Connection accepted.');
// send updates history to new client
if (history.length > 0) {
connection.sendUTF(JSON.stringify( { type: 'history', data: history} ));
}
// new stock update
connection.on('message', function(message) {
if (message.type === 'utf8') { // accept only text
if (enteredPassword === false) { // Login.
enteredPassword = handleHtmlEntities(message.utf8Data);
if (enteredPassword === SYSTEM_PASSWORD)
{
connection.sendUTF(JSON.stringify({ type:'login', data: 'black', status: 'success' }));
console.log((new Date()) + 'Admin Logged In');
}else {
enteredPassword = false;
connection.sendUTF(JSON.stringify({ type:'login', data: 'black', status: 'failure' }));
console.log((new Date()) + 'Admin login failed');
}
} else { // log and broadcast the update
console.log((new Date()) + ' Received Message from Admin : '+ message.utf8Data);
// to keep all updates in history
var obj = {
time: (new Date()).getTime(),
text: handleHtmlEntities(message.utf8Data),
author: 'Admin'
};
history.push(obj);
history = history.slice(-100);
// broadcast message to all connected clients
var json = JSON.stringify({ type:'message', data: obj });
for (var i=0; i < clients.length; i++) {
clients[i].sendUTF(json);
}
}
}
});
// client disconnected
connection.on('close', function(connection) {
if (enteredPassword !== false ) {
console.log((new Date()) + " Peer "
+ connection.remoteAddress + " disconnected.");
// remove client from the list of connected clients
clients.splice(index, 1);
}
});
});