-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (46 loc) · 1.55 KB
/
index.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
const express = require('express');
const r = require('rethinkdb');
const WebSocket = require('ws');
const app = express();
app.use('/', express.static('public'));
r.connect( {host: 'localhost', port: 28015}, (err, connection) => {
if (err) throw err;
const wss = new WebSocket.Server({ port: 3001 });
wss.on('connection', ws => {
ws.on('message', raw => {
var data = JSON.parse(raw);
r.db('chat').table('messages').insert({
time: Date.now(),
from: data.from,
message: data.message
}).run(connection, err => {
if (err) throw err;
});
});
});
r.db('chat').table('messages').changes().run(connection, (err, cursor) => {
if (err) throw err;
cursor.each((err, row) => {
if (err) throw err;
if(row.new_val) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(row.new_val));
}
});
}
});
});
app.get('/old-messages', (req, res) => {
r.db('chat').table('messages').orderBy(r.desc('time')).limit(10).run(connection, (err, cursor) => {
if (err) throw err;
cursor.toArray((err, result) => {
if (err) throw err;
res.send(result);
});
});
});
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});