-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (70 loc) · 2.26 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
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
const express = require('express')
const { createServer} = require('node:http')
const { Server } = require('socket.io')
const {join} = require('node:path')
const sqlite3 = require('sqlite3')
const {open} = require('sqlite')
async function main(){
const db = await open({
filename: 'chat.db',
driver: sqlite3.Database
})
await db.exec(`
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
client_offset TEXT UNIQUE,
content TEXT
);
`)
const app = express()
const server = createServer(app)
const io = new Server(server, {connectionStateRecovery: {}})
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'index.html'));
})
io.emit('hello', 'world') //emits event to all connected sockets
io.on('connection', async (socket) => {
socket.on('chat message', async (msg, clientOffset, callback) => {
let result;
try {
result = await db.run('INSERT INTO messages (content, client_offset) VALUES (?, ?)', msg, clientOffset)
} catch (error) {
if (error.errno === 19 /* SQLITE_CONSTRAINT */ ) {
// the message was already inserted, so we notify the client
callback();
} else {
// nothing to do, just let the client retry
}
return;
}
io.emit('chat message', msg, result.lastID);
callback()
});
socket.on('connection message', async (msg) => {
let result;
try {
result = await db.run('INSERT INTO messages (content) VALUES (?)', msg)
} catch (error) {
throw new Error(error);
}
io.emit('connection message', msg, result.lastID);
});
if (!socket.recovered) {
// if the connection state recovery was not successful
try {
await db.each('SELECT id, content FROM messages WHERE id > ?',
[socket.handshake.auth.serverOffset || 0],
(_err, row) => {
socket.emit('chat message', row.content, row.id);
}
)
} catch (e) {
throw new Error(e)
}
}
});
server.listen(3000, () => {
console.log("listening at port http://localhost:3000");
})
}
main();