-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket-server.js
86 lines (67 loc) · 2.38 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
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
'use strict';
var socketIO = require('socket.io');
var ot = require('ot');
var UsersList = require('./helpers/userslist').UsersList;
var roomList = {};
var usersList;
module.exports = function(server) {
var str = '// Welcome \n\n' + 'function helloWorld() {\n console.log("Hello World!"); \n} \n\nhelloWorld();';
var io = socketIO(server);
io.on('connection', function(socket) {
socket.on('joinRoom', function(data) {
if (!roomList[data.room]) {
usersList = new UsersList();
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;
}
socket.join(data.room);
usersList.removeUser(socket.id); // remove user from other active tasks
usersList.addUser(socket.id, data.username, data.room); // add user to task
io.to(data.room).emit('updateUserList', usersList.getUserList(data.room)); // emit event
roomList[data.room].addClient(socket);
roomList[data.room].setName(socket, data.username);
socket.broadcast.to(data.room).emit('chatMessage', {
username: 'Admin',
message: data.username + ' has joined.'
});
});
socket.on('chatMessage', function(data) {
var user = usersList.getUser(socket.id);
if (user && isRealString(data.message)) {
io.to(user.room).emit('chatMessage', data);
}
});
socket.on('typing', function(data) {
var user = usersList.getUser(socket.id);
var response = data;
if (data) {
response = user.name;
}
socket.broadcast.to(user.room).emit('typing', response);
});
socket.on('disconnect', function() {
if (usersList) {
var user = usersList.removeUser(socket.id);
if (user) {
socket.leave(user.room);
io.to(user.room).emit('updateUserList', usersList.getUserList(user.room));
io.to(user.room).emit('chatMessage', {
username: 'Admin',
message: user.name + ' has left.'
});
}
}
});
});
}
var isRealString = function(str) {
return typeof str === 'string' && str.trim().length > 0;
};