这对我有用:
# server/methods/app.coffee
#---------------------------------------------------
# Info found in Meteor documentation (24 Feb. 2015)
#
# > Currently when a client reconnects to the server
# (such as after temporarily losing its Internet connection),
# it will get a new connection each time. The onConnection callbacks will be called again,
# and the new connection will have a new connection id.
# > In the future, when client reconnection is fully implemented,
# reconnecting from the client will reconnect to the same connection on the server:
# the onConnection callback won't be called for that connection again,
# and the connection will still have the same connection id.
#
# In order to avoid removing data from persistent collections (ex. cartitems) associated with client sessionId (conn.id), at the moment we'll implement the next logic:
#
# 1. Add the client IP (conn.clientAddress) to the cartitems document (also keep the conn.id)
# 2. When a new connection is created, we find the cartitems associated with this conn.clientAddress and we'll update (multi: true) the conn.id on the all cartitems.
# 3. Only remove the cartitems associated with the conn.id after 10 seconds (if in this time the customer reconnect, the conn.id will have been updated at the point #2. we avoid this way removing after refresh the page for example.
# 4. After 10 seconds (ex. the user close the window) we'll remove the cartitems associated with the conn.id that it has been closed.
Meteor.onConnection (conn) ->
CartItems.update({clientAddress: conn.clientAddress}, {$set: {sessionId: conn.id}}, {multi: true})
conn.onClose ->
Meteor.setTimeout ->
CartItems.remove {sessionId: conn.id}
, 10000
Meteor.methods
getSessionData: ->
conn = this.connection
{sessionId: conn.id, clientAddress: conn.clientAddress}