-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
118 lines (105 loc) · 3.03 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
const http = require('http');
const fs = require('fs');
const WebSocketServer = require('ws').Server;
const xss = require('xss');
// const AlchemyAPI = require('watson-developer-cloud/alchemy-language/v1');
// const key = process.env.ALCHEMY_KEY || null;
// const alchemy_client = key ? new AlchemyAPI({api_key:key}) : null;
let currentUsers = 0;
const messageHistory = [];
const port = process.env.PORT || 80;
// Basic HTTP httpServer
const httpServer = http.createServer((req, res) => {
switch (req.url) {
case '/':
fs.readFile('public/index.html',
'utf-8',
(err, data) => res.end(data));
break;
case '/logo.png':
fs.readFile('public/logo.png', (err, data) => res.end(data));
break;
case '/bundle.js':
fs.readFile('public/bundle.js',
'utf-8',
(err, data) => res.end(data));
break;
default:
res.end();
}
});
const webSocketServer = new WebSocketServer({server: httpServer});
webSocketServer.on('connection', (ws) => {
const payload = {
message_type: 'initial_message_load',
payload: messageHistory,
};
// Initial history
ws.send(JSON.stringify(payload));
ws.on('open', () => {
console.log("Opening a connection...");
});
ws.on('message', (msg) => {
const clientReply = JSON.parse(msg);
switch (clientReply.cmd) {
case 'connect':
currentUsers++; break;
case 'user_count':
ws.send(JSON.stringify({
users_count: `${currentUsers > 0 ? currentUsers : 0}`,
message_type: 'user_count',
}));
break;
case 'new_message':
clientReply.payload = xss(clientReply.payload);
messageHistory.push(clientReply.payload);
const trimmed = clientReply.rawMessage.trim();
const sendMeOff = JSON.stringify({
message_type: 'new_chat_message',
payload: clientReply.payload,
});
webSocketServer.clients.forEach((client) => {
try {
// if (trimmed.startsWith('!hyebot.sentiment=>')) {
// const analyze =
// {text:trimmed.split('=>')[1].trim()};
// if (alchemy_client) {
// alchemy_client.sentiment(analyze, (e, resp) => {
// client.send(sendMeOff);
// if (e)
// client.send(JSON.stringify({
// message_type:'new_chat_message',
// payload:`Watson messed up ${JSON.stringify(e)}`
// }));
// else {
// const bot_message =
// `{message:${trimmed}} has a score of:${resp.docSentiment.score} and its ${resp.docSentiment.type}`;
// client.send(JSON.stringify({
// message_type:'new_chat_message',
// payload:bot_message
// }));
// }
// });
// }
// } else {
client.send(sendMeOff);
// }
} catch (e) {
console.error('Tried writing to a closed socket', e);
}
});
break;
default:
break;
}
});
ws.on('close', () => {
currentUsers--;
});
ws.on('error', (evt) => {
console.log("ERR: " + evt.data);
});
});
httpServer.listen(port, () =>
console.log(`Server started on port:${port}, check localhost:${port}`)
);