-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathyjs-ws-server.js
115 lines (101 loc) · 2.96 KB
/
yjs-ws-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
'use strict'
// @todo: Refactor / clean up code.
// @todo: Combine the two debugs into one.
const debug = require('debug')('YjsWS')
const Y = require('yjs')
// @todo: Verify what these two lines are doing.
debug(Y)
debug('Y.debug:', Y.debug)
const minimist = require('minimist')
require('y-memory')(Y)
try {
require('y-leveldb')(Y)
} catch (err) {}
try {
require('./y-websockets-server.js')(Y) // Doesn't exist...
} catch (err) {
require('y-websockets-server')(Y) // <-- ...this one is called.
}
global.yInstances = {}
module.exports = (io, socket) => {
debug('[NEXT LINE: "const options = minimist(process.argv.slice(2), {..."]')
debug(' process.argv=', process.argv)
const options = minimist(process.argv.slice(2), {
string: ['port', 'debug', 'db'],
default: {
port: process.env.PORT || 3000,
debug: false,
db: 'memory'
}
})
const getInstanceOfY = function (room) {
debug(`[getInstanceOfY(room)] arguments: `, arguments)
if (global.yInstances[room] == null) {
debug('[NEXT LINE: " global.yInstances[room] = Y({..."]')
global.yInstances[room] = Y({
db: {
name: options.db,
dir: 'y-leveldb-databases',
namespace: room
},
connector: {
name: 'websockets-server',
room: room,
io: io,
debug: !!options.debug
},
share: {}
})
}
debug('[NEXT LINE: "return global.yInstances[room];"]')
return global.yInstances[room]
}
let rooms = []
socket.on('joinRoom', (room) => {
debug(`[socket.on('joinRoom', fn)] room: ${room}`)
debug('User "%s" joins room "%s"', socket.id, room)
socket.join(room)
getInstanceOfY(room).then((y) => {
if (rooms.indexOf(room) === -1) {
y.connector.userJoined(socket.id, 'slave')
rooms.push(room)
}
})
})
socket.on('yjsEvent', (msg) => {
debug(`[socket.on('yjsEvent', fn)] msg: ${msg}`)
if (msg.room != null) {
getInstanceOfY(msg.room).then((y) => {
debug('[PREV LINE: "getInstanceOfY(msg.room).then((y) => {"...]')
y.connector.receiveMessage(socket.id, msg)
})
}
})
socket.on('disconnect', () => {
debug(`[socket.on('disconnect', fn)]`)
for (var i = 0; i < rooms.length; i++) {
let room = rooms[i]
getInstanceOfY(room).then((y) => {
debug('[PREV LINE: "getInstanceOfY(msg.room).then((y) => {"...]')
var i = rooms.indexOf(room)
if (i >= 0) {
y.connector.userLeft(socket.id)
rooms.splice(i, 1)
}
})
}
})
socket.on('leaveRoom', (room) => {
debug(`[socket.on('leaveRoom')] room: ${room}`)
getInstanceOfY(room).then((y) => {
debug('[PREV LINE: "getInstanceOfY(msg.room).then((y) => {"...]')
var i = rooms.indexOf(room)
if (i >= 0) {
y.connector.userLeft(socket.id)
rooms.splice(i, 1)
}
})
})
// @todo: Check if this return value gets used anywhere.
return module
}