Updated realtime.js, use socket.io built-in broadcast function instead self iterate to avoid extra issues

This commit is contained in:
Wu Cheng-Han 2015-09-24 11:36:41 +08:00
parent e6cac820ae
commit 950d978fe8
2 changed files with 26 additions and 0 deletions

3
app.js
View File

@ -65,6 +65,9 @@ mongoose.connect(process.env.MONGOLAB_URI || config.mongodbstring);
var db = require("./lib/db.js");
var realtime = require("./lib/realtime.js");
//assign socket io to realtime
realtime.io = io;
//methodOverride
app.use(methodOverride('_method'));

View File

@ -26,6 +26,7 @@ var User = require("./user.js");
//public
var realtime = {
io: null,
onAuthorizeSuccess: onAuthorizeSuccess,
onAuthorizeFail: onAuthorizeFail,
secure: secure,
@ -67,10 +68,13 @@ function emitCheck(note) {
var out = {
updatetime: note.updatetime
};
realtime.io.to(note.id).emit('check', out);
/*
for (var i = 0, l = note.socks.length; i < l; i++) {
var sock = note.socks[i];
sock.emit('check', out);
};
*/
}
//actions
@ -183,23 +187,29 @@ function emitOnlineUsers(socket) {
users: users
};
out = LZString.compressToUTF16(JSON.stringify(out));
realtime.io.to(notename).emit('online users', out);
/*
for (var i = 0, l = notes[notename].socks.length; i < l; i++) {
var sock = notes[notename].socks[i];
if (sock && out)
sock.emit('online users', out);
};
*/
}
function emitUserStatus(socket) {
var notename = getNotenameFromSocket(socket);
if (!notename || !notes[notename]) return;
var out = buildUserOutData(users[socket.id]);
socket.broadcast.to(notename).emit('user status', out);
/*
for (var i = 0, l = notes[notename].socks.length; i < l; i++) {
var sock = notes[notename].socks[i];
if (sock != socket) {
sock.emit('user status', out);
}
};
*/
}
function emitRefresh(socket) {
@ -287,6 +297,7 @@ function startConnection(socket) {
var updatetime = data.rows[0].update_time;
var server = new ot.EditorSocketIOServer(body, [], notename, ifMayEdit);
notes[notename] = {
id: notename,
owner: owner,
permission: note.permission,
socks: [],
@ -522,10 +533,13 @@ function connection(socket) {
var out = {
permission: permission
};
realtime.io.to(note.id).emit('permission', out);
/*
for (var i = 0, l = note.socks.length; i < l; i++) {
var sock = note.socks[i];
sock.emit('permission', out);
};
*/
});
});
}
@ -569,12 +583,15 @@ function connection(socket) {
if (!notename || !notes[notename]) return;
users[socket.id].cursor = data;
var out = buildUserOutData(users[socket.id]);
socket.broadcast.to(notename).emit('cursor focus', out);
/*
for (var i = 0, l = notes[notename].socks.length; i < l; i++) {
var sock = notes[notename].socks[i];
if (sock != socket) {
sock.emit('cursor focus', out);
}
};
*/
});
//received cursor activity
@ -583,12 +600,15 @@ function connection(socket) {
if (!notename || !notes[notename]) return;
users[socket.id].cursor = data;
var out = buildUserOutData(users[socket.id]);
socket.broadcast.to(notename).emit('cursor activity', out);
/*
for (var i = 0, l = notes[notename].socks.length; i < l; i++) {
var sock = notes[notename].socks[i];
if (sock != socket) {
sock.emit('cursor activity', out);
}
};
*/
});
//received cursor blur
@ -599,12 +619,15 @@ function connection(socket) {
var out = {
id: socket.id
};
socket.broadcast.to(notename).emit('cursor blur', out);
/*
for (var i = 0, l = notes[notename].socks.length; i < l; i++) {
var sock = notes[notename].socks[i];
if (sock != socket) {
sock.emit('cursor blur', out);
}
};
*/
});
//when a new client disconnect