-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecords.js
62 lines (50 loc) · 1.66 KB
/
records.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
"use strict";
const bugs = require('./bugs.js')
const { Server } = require("socket.io");
const redis = require('redis').createClient({
url: (process.env.REDIS_URL || 'redis://localhost:6379/0')
});
var assumed_app_url = "https://"+ (process.env.CANONICAL_HOST || (process.env.VIRTUAL_HOST || '').replace('channels.',''))
const config = {
port: (process.env.PORT || 5000),
allowedOrigin: (process.env.APP_URL || assumed_app_url),
}
console.log("Allowed origin", config.allowedOrigin);
module.exports = async () => {
try {
const io = new Server(config.port, {
connectionStateRecovery: {
maxDisconnectionDuration: 30 * 60 * 1000,
skipMiddlewares: true,
},
cors: {
origin: config.allowedOrigin,
credentials: true
}
})
redis.on('error', (err) => bugs.log(err) );
await redis.connect();
const redisSub = redis.duplicate();
await redisSub.connect();
await redisSub.subscribe('/records', (json, channel) => {
let data = JSON.parse(json)
io.to(data.room).emit('records', data)
})
await redisSub.subscribe('/system_notice', (json, channel) => {
io.emit('notice', JSON.parse(json))
})
io.on("connection", async (socket) => {
socket.join("notice")
let channel_token = socket.handshake.query.channel_token
let user = await redis.get("/current_users/"+channel_token)
if (user) {
user = JSON.parse(user)
socket.join("user-"+user.id)
user.group_ids.forEach(groupId => { socket.join("group-"+groupId) })
console.log("have current user!", user.name, user.group_ids)
}
})
} catch (e) {
bugs.log(e)
}
}