-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-server.js
46 lines (40 loc) · 1.18 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
43
44
45
46
"use strict";
var socketIO = require("socket.io");
var ot = require("ot");
var roomList = {};
module.exports = function(server) {
var str = "This is markdown \n\n" + " var i = i + 1; ";
var io = socketIO(server);
io.on("connection", function(socket) {
socket.on("chatMessage", function(data) {
io.to(socket.room).emit("chatMessage", data);
});
socket.on("joinRoom", function(data) {
if (!roomList[data.room]) {
var socketIOServer = new ot.EditorSocketIOServer(
str,
[],
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); //initialising room using roomId
});
socket.on("disconnect", function() {
socket.leave(socket.room);
});
});
};