summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorWu Cheng-Han2015-09-24 11:36:41 +0800
committerWu Cheng-Han2015-09-24 11:36:41 +0800
commit950d978fe81eace56453894a02526ce252a09d33 (patch)
treeadd41373d95c5ebeeed316691fa8af1f50fafb3c /lib
parente6cac820ae9e127b88a0bcbc008c40fe918247dc (diff)
Updated realtime.js, use socket.io built-in broadcast function instead self iterate to avoid extra issues
Diffstat (limited to 'lib')
-rw-r--r--lib/realtime.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/realtime.js b/lib/realtime.js
index 0b5a46b2..b5a3b182 100644
--- a/lib/realtime.js
+++ b/lib/realtime.js
@@ -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