-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-server.js
42 lines (33 loc) · 1.05 KB
/
socket-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
'use strict';
var socketIO = require('socket.io');
var ot = require('ot');
var roomList = {};
module.exports = function(server) {
var str = 'This is a Markdown heading \n\n' +
'var i = i + 1;';
var io = socketIO(server);
io.on('connection', function(socket) {
socket.on('joinRoom', function(data) {
if (!roomList[data.room]) {
var socketIOServer = new ot.EditorSocketIOServer(str, [], data.room, function(socket, cb) {
var self = this;
Task.findByIdAndUpdate(data.room, {content: self.document}, function(err) {
if (err) return cb(false);
cb(true);
});
});
roomList[data.room] = socketIOServer;
}
roomList[data.room].addClient(socket);
roomList[data.room].setName(socket, data.username);
socket.room = data.room;
socket.join(data.room);
});
socket.on('chatMessage', function(data) {
io.to(socket.room).emit('chatMessage', data);
});
socket.on('disconnect', function() {
socket.leave(socket.room);
});
})
}