5

Simple question, maybe simple answer: how do I know on the server that a certain client has disconnected? Basic use case: the serve would need to know if a player has dropped the connection.

Bence
  • 323
  • 1
  • 6
  • 12
  • 1
    http://stackoverflow.com/questions/10257958/server-cleanup-after-a-client-disconnects – Vic Goldfeld Sep 11 '12 at 13:30
  • just added an alternative way of tracking user connection status on this thread http://stackoverflow.com/questions/10257958/server-cleanup-after-a-client-disconnects – Lloyd Sep 12 '12 at 09:19

5 Answers5

4

In the publish function, you can watch socket close event as follows.

this.session.socket.on "close", -> # do your thing
poordeveloper
  • 2,272
  • 1
  • 23
  • 36
4
 Meteor.publish("yourPublishFunction", function()
        {
            var id = this._session.userId;
            this._session.socket.on("close", Meteor.bindEnvironment(function()
            {
              console.log(id); // called once the user disconnects
            }, function(e){console.log(e)}));

            return YourCollection.find({});
        });
Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
3

I've created a pretty comprehensive package to keep track of all logged-in sessions from every user, as well as their IP addresses and activity:

https://github.com/mizzao/meteor-user-status

To watch for disconnects, you can just do the following, which catches both logouts and browser closes:

UserStatus.on "sessionLogout", (userId, sessionId) ->
  console.log(userId + " with session " + sessionId + " logged out")

You can also check out the code and do something similar for yourself.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
1

Maybe (in the server code)

Meteor.default_server.sessions.length

or

Meteor.default_server.stream_server.open_sockets.length
snez
  • 2,400
  • 23
  • 20
0

you can do one thing make a event on server and call it from browser with ajax which call after some small time interval settimeinterval using with session values into header and if server din`t get request from user it means he dropped connection