Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve tracking of multiple connections
- Loading branch information
Showing
3 changed files
with
29 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,3 @@ | ||
|
||
# Hook into original logout function | ||
Meteor.__logout__ = Meteor.logout | ||
Meteor.logout = (callback) -> | ||
Meteor.users.update Meteor.userId(), | ||
$set: {'profile.online': false} | ||
Meteor.__logout__(callback) | ||
|
||
# Add online status when logged in or resuming | ||
Deps.autorun -> | ||
return unless Meteor.userId() | ||
Meteor.users.update Meteor.userId(), | ||
$set: {'profile.online': true} | ||
|
||
Meteor.subscribe "statusWatcher" | ||
Meteor.userId() # Re-subscribe whenever logging in our out | ||
Meteor.subscribe "statusWatcher" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
this.UserSockets = new Meteor.Collection(null) | ||
|
||
# Publication is guaranteed to be called once per connection | ||
# trick as referenced in http://stackoverflow.com/q/10257958/586086 | ||
# pub/sub trick as referenced in http://stackoverflow.com/q/10257958/586086 | ||
Meteor.publish "statusWatcher", -> | ||
id = @_session.userId | ||
return unless id? | ||
return unless @_session.socket? | ||
sockId = @_session.socket.id | ||
|
||
# Add socket to open connections | ||
Meteor.bindEnvironment -> | ||
UserSockets.insert | ||
userId: id | ||
# Untrack connection on logout | ||
unless id? | ||
# TODO: this could be replaced with a findAndModify once it's supported on Collections | ||
existing = UserSockets.findOne | ||
sockId: sockId | ||
, (e) -> | ||
Meteor._debug "Exception from connection add callback:", e | ||
return unless existing? # Probably new session | ||
|
||
id = existing.userId | ||
UserSockets.remove | ||
sockId: sockId | ||
|
||
if UserSockets.find(userId: id).count() is 0 | ||
Meteor.users.update id, | ||
$set: {'profile.online': false} | ||
return | ||
|
||
# Add socket to open connections | ||
UserSockets.insert | ||
userId: id | ||
sockId: sockId | ||
Meteor.users.update id, | ||
$set: {'profile.online': true} | ||
|
||
# Remove socket on close | ||
@_session.socket.on "close", Meteor.bindEnvironment( -> | ||
@_session.socket.on "close", Meteor.bindEnvironment -> | ||
This comment has been minimized.
Sorry, something went wrong. |
||
UserSockets.remove | ||
userId: id | ||
sockId: sockId | ||
|
||
if UserSockets.find(userId: id).count() is 0 | ||
Meteor.users.update id, | ||
$set: {'profile.online': false} | ||
|
||
, (e) -> | ||
Meteor._debug "Exception from connection close callback:", e | ||
) | ||
|
||
return |
This needs to be replaced with the new http://docs.meteor.com/#meteor_onconnection in order to take advantage of the DDP heartbeat timeout.