-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (26 loc) · 868 Bytes
/
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
/**
* websocket implementation
* of joseph weizenbaum's eliza
*/
const ElizaBot = require("elizabot");
const WebSocket = require("ws")
var eliza = new ElizaBot();
const server = new WebSocket.Server({
port: 8020
})
let sockets = []
server.on("connection", function(socket) {
sockets.push(socket); // lock it in
socket.on("message", function(msg) {
// rudimentary logging
console.log(new Date().toLocaleTimeString() + ": " + msg)
// channel things to eliza correctly
if (msg === "init") sockets.forEach(s => s.send(eliza.getInitial()))
else if (msg === "quit") sockets.forEach(s => s.send(eliza.reset()))
else sockets.forEach(s => s.send(eliza.transform(msg)))
});
socket.on("close", function() {
eliza.reset() // reset
sockets = sockets.filter(s => s !== socket)
});
});