forked from togiberlin/codasession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-server.js
40 lines (33 loc) · 1.12 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
'use strict';
var socketIO = require('socket.io');
var ot = require('ot');
var roomList = {};
module.exports = function(server) {
var str = '/** Hello there! You can start to type your code here. For example: */ \n\n' +
'var greeting = "Hello world!";';
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;
Session.findByIdAndUpdate(data.room, { sourceCode: 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);
});
})
}