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 db = require("./lib/db.js");
var realtime = require("./lib/realtime.js"); var realtime = require("./lib/realtime.js");
//assign socket io to realtime
realtime.io = io;
//methodOverride //methodOverride
app.use(methodOverride('_method')); app.use(methodOverride('_method'));

View file

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