From 08fa853fca1706a9bc69edd62188d5d4e8f322de Mon Sep 17 00:00:00 2001 From: Nick Martin Date: Tue, 7 Feb 2012 15:57:17 -0800 Subject: [PATCH] Implement Live Data protocol: 'connect' and 'connected'. For now, high-level behavior is the same. When the transport disconnects, client doesn't attempt to reuse previous session. Server always establishes a new Live Data session with each connect. Server does not support method reply cache, and won't honor a client's attempt to reuse a previous Live Data session id. --- packages/livedata/livedata_client.js | 15 ++++++++++----- packages/livedata/livedata_server.js | 9 ++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/livedata/livedata_client.js b/packages/livedata/livedata_client.js index 3f5fddc707..974a90f8f2 100644 --- a/packages/livedata/livedata_client.js +++ b/packages/livedata/livedata_client.js @@ -38,6 +38,7 @@ if (typeof Meteor === "undefined") Meteor = {}; }); var livedata_connected = function (msg) { + Meteor._debug("CONNECTED", msg); }; var livedata_data = function (msg) { @@ -87,13 +88,17 @@ if (typeof Meteor === "undefined") Meteor = {}; }); Meteor._stream.reset(function (msg_list) { - // remove existing subscribe and unsubscribe - msg_list = _.reject(msg_list, function (elem) { - return (!elem - || (elem[0] === "livedata" && elem[1] - && (elem[1].msg === "sub" || elem[1].msg === "unsub"))); + // remove all 'livedata' message except 'method' + msg_list = _.filter(msg_list, function (elem) { + return (elem && (elem[0] !== "livedata" || + (elem[1] && elem[1].msg === "method"))); }); + // Send a connect message at the beginning of the stream. + // NOTE: reset is called even on the first connection, so this is + // the only place we send this message. + msg_list.unshift(['livedata', {msg: 'connect'}]); + // add new subscriptions at the end. this way they take effect after // the handlers and we don't see flicker. subs.find().forEach(function (sub) { diff --git a/packages/livedata/livedata_server.js b/packages/livedata/livedata_server.js index 938a4795a0..87302bd843 100644 --- a/packages/livedata/livedata_server.js +++ b/packages/livedata/livedata_server.js @@ -119,6 +119,11 @@ if (typeof Meteor === "undefined") Meteor = {}; }).run(); }; + var livedata_connect = function (socket, msg) { + // Always start a new session. We don't support any reconnection. + socket.emit('livedata', {msg: 'connected', session: Meteor.uuid()}); + }; + var livedata_sub = function (socket, msg) { if (!publishes[msg.name]) { // can't sub to unknown publish name @@ -195,7 +200,9 @@ if (typeof Meteor === "undefined") Meteor = {}; return; } - if (msg.msg === 'sub') + if (msg.msg === 'connect') + livedata_connect(socket, msg); + else if (msg.msg === 'sub') livedata_sub(socket, msg); else if (msg.msg === 'unsub') livedata_unsub(socket, msg);