User disconnects

141 views
Skip to first unread message

carles

unread,
Sep 1, 2011, 9:32:53 AM9/1/11
to ape-p...@googlegroups.com
How can I know when a user disconnects on real time??

Right now, Server waits for long polling response. When long polling finishes, then it knows user is disconnected.

tia.

Jacobus brogly.decap

unread,
Sep 1, 2011, 9:45:00 AM9/1/11
to ape-p...@googlegroups.com
Disconnecting, the underlying socket receives  FIN frame, the epoll unix function should be able to detect this

http://www.freesoft.org/CIE/Course/Section4/11.htm

Case 2: TCP receives a FIN from the network

    If an unsolicited FIN arrives from the network, the receiving TCP
    can ACK it and tell the user that the connection is closing.  The
    user will respond with a CLOSE, upon which the TCP can send a FIN to
    the other TCP after sending any remaining data.  The TCP then waits
    until its own FIN is acknowledged whereupon it deletes the
    connection.  If an ACK is not forthcoming, after the user timeout
    the connection is aborted and the user is told.

2011/9/1 carles <bal...@gmail.com>
How can I know when a user disconnects on real time??

Right now, Server waits for long polling response. When long polling finishes, then it knows user is disconnected.

tia.

--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

Louis Charette

unread,
Sep 1, 2011, 2:58:04 PM9/1/11
to ape-p...@googlegroups.com
Hi there,

If you find a solution, let me know. I must admit it would be nice to have this kind of feature for my chat or for anybody using ape to write game. With that, you don't have to way 2 minutes after your opponent leave before you see him disappear in the game/chat.

I read something here about the timeout that could be changed before building ape from sources. Maybe it's something worth looking for.

  - Louis

Envoyé de mon iPhone
--

Pablo Tejada

unread,
Sep 1, 2011, 3:10:28 PM9/1/11
to ape-p...@googlegroups.com

This is simple to do in JavaScript, you could use the window onbeforeunload event to trigger the left command before the user either quits or navigates away from the webapp

Jacobus brogly.decap

unread,
Sep 1, 2011, 4:30:55 PM9/1/11
to ape-p...@googlegroups.com
for now you can have frontend send a "keepalive" message, but be sure to disable "nagle algorithm" on your websocket (google it)


2011/9/1 Pablo Tejada <ptej...@gmail.com>

This is simple to do in JavaScript, you could use the window onbeforeunload event to trigger the left command before the user either quits or navigates away from the webapp

--

Louis Charette

unread,
Sep 1, 2011, 8:04:36 PM9/1/11
to APE Project
I tried the "onbeforeunload" event. Looks like it's not working for
Chrome & Firefox. They both need "onunload", but this one doesn't work
on Safari (Didn't tried "onbeforeunload" on safari)

It both doesn't fire when you simply close the page though… But I must
agree it's the simplest solution and better than nothing :)

- Louis

Louis Charette

unread,
Sep 1, 2011, 8:26:00 PM9/1/11
to APE Project
False. They both fire, but it looks like it's the RAW I'm trying to
send that is not working…

Wes Garland

unread,
Sep 1, 2011, 9:37:23 PM9/1/11
to ape-p...@googlegroups.com
Does the RAW actually get sent over the wire?  Check with a packet sniffer if you're not sure.

Wes

--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/



--
Wesley W. Garland
Director, Product Development
PageMail, Inc.
+1 613 542 2787 x 102

Louis Charette

unread,
Sep 1, 2011, 10:41:26 PM9/1/11
to ape-p...@googlegroups.com
I changed the raw for a custom server function:

Ape.registerCmd('leavePage', true, function(params, infos) {
     Ape.log("Leaving Page");
});


Client side (I used Jquery) :


$(window).unload(function() {
  console.log("Just left page");
ape.request.send('leavePage', {});
});


If I reload the page with Chrome, "Leaving Page" is addend to the APE logs, but with Safari the log entry is not there.

  - Louis

Pablo Tejada

unread,
Sep 1, 2011, 11:44:32 PM9/1/11
to ape-p...@googlegroups.com
I knew about unload and safari, is why suggested the onbeforeunload event. Have not tried my self but I read it works. Try raw JS like

window.onbeforeunload = function(){
    //send left command
}

Sent from my iPad

Louis Charette

unread,
Sep 2, 2011, 4:25:17 PM9/2/11
to ape-p...@googlegroups.com
Yeah well I already tried this: 

$(window).bind('beforeunload', function() {

but I found the other one was working better, with Chrome at least.
I just tried the raw js you quoted and it's not working either.

  - Louis

Louis Charette

unread,
Sep 15, 2011, 4:55:02 PM9/15/11
to APE Project
For a reason or another, my final answer never made it here... So here
it is:

- Louis

---------------------

FOUND IT !

Here : http://stackoverflow.com/questions/6745323/alternative-of-onbeforeunload-any-idea/6745584#6745584

"OnBeforeUnload is supported in Safari - what is not supported is the
use of AJAX ... you need to use a Synchronous call - you can use alert
to verify this....."

So I changed my code for something like this :

window.onbeforeunload = function(){
$.ajax({
type: "POST",
url: [...],
async: false,
data: [...],
});
}

The Ajax query (Still using Jquery) calls a php file who then call a
custom command on the server (based on inlinepush) and the server take
care of the rest. Unless an APE command could be call by the AJAX
request itself...?

Anyway, it's not the prettiest solution, but it's works in Chrome,
Firefox AND Safari (Didn't tried IE yet, on a Mac :P) and it seems to
works whatever way you're leaving the page. The other way to make this
work browsers would be to make sure all RAW or CMD are send to APE
using a synchronous call (or provide the option to have a particular
command/raw be sent this way), am I right?

Anyway, I say close enough for now lol. If someone is interested to
see all the code I used to add in his own app, just asked me. Or if
you have a prettier solution... ^_^.

Thanks everyone,

- Louis


P.S.: I hope Carles is still reading this. I kind of stole his
question lol.

Nicolas

unread,
Sep 15, 2011, 5:08:39 PM9/15/11
to APE Project
Hi Louis,

Thx for you reply in the other thread.

I actually want to send the command from php, but my problem is that I
don't know what command to send. A custom command was my initial
thought, but then once you receive this command on the server, how do
you link it with the user ask the system to delete the user...

So, I am interested in seeing your code, I believe the answer is
there.

Regards.

Nicolas.


On 15 sep, 21:55, Louis Charette <charette.lo...@gmail.com> wrote:
> For a reason or another, my final answer never made it here... So here
> it is:
>
>   - Louis
>
> ---------------------
>
> FOUND IT !
>
> Here :http://stackoverflow.com/questions/6745323/alternative-of-onbeforeunl...

Nicolas

unread,
Sep 15, 2011, 5:13:59 PM9/15/11
to APE Project
By the way, I may be missing something, but in your case, why don't
you just use this.core.quit() on unbeforeunload?



On 2 sep, 21:25, Louis Charette <charette.lo...@gmail.com> wrote:
> Yeah well I already tried this:
>
>         $(window).bind('beforeunload', function() {
>
> but I found the other one was working better, with Chrome at least.
> I just tried the raw js you quoted and it's not working either.
>
>   - Louis
>
> Le 2011-09-01 à 23:44, Pablo Tejada a écrit :
>
>
>
>
>
>
>
> > I knew about unload and safari, is why suggested the onbeforeunload event. Have not tried my self but I read it works. Try raw JS like
>
> > window.onbeforeunload = function(){
> >      //send left command
> >  }
>
> > Sent from my iPad
>
> > On Sep 1, 2011, at 10:41 PM, Louis Charette <charette.lo...@gmail.com> wrote:
>
> >> I changed the raw for a custom server function:
>
> >> Ape.registerCmd('leavePage', true, function(params, infos) {
> >>      Ape.log("Leaving Page");
> >> });
>
> >> Client side (I used Jquery) :
>
> >> $(window).unload(function() {
> >>        console.log("Just left page");
> >>        ape.request.send('leavePage', {});
> >> });
>
> >> If I reload the page with Chrome, "Leaving Page" is addend to the APE logs, but with Safari the log entry is not there.
>
> >>   - Louis
>
> >> Le 2011-09-01 à 21:37, Wes Garland a écrit :
>
> >>> Does the RAW actually get sent over the wire?  Check with a packet sniffer if you're not sure.
>
> >>> Wes
>

Louis Charette

unread,
Sep 15, 2011, 6:42:21 PM9/15/11
to APE Project
this.core.quit(); doesn't fire a "userLeft" event. You still have to
wait for the timeout for the left event to be fired. If I try this
without actully closing the window, I can't send or received anything,
but the other clients are not informed right away that I left.

In my chat application, when I call quit, the other clients will still
see the leaving user as connected until the timeout is reached. I
tried to send a "left" command, but it won't work in all browsers
(Safari) because of what I explained in a previous post (As any other
direct Ape command)


Depending on what you want to achieve, there are multiple way to do
it. What I did is that in "onbeforeunload", with a Synchronous AJAX
request, I call a php file who then send a command (using something
similar to inlinepush) to the server, along with the user pubid. With
this pubid, I can retrieve the user's info on the server and call a
left command for the channel:

Ape.getUserByPubid(params.data.pubid).left('testsalon')

Again, depending of what you are trying to do, once PHP sent the
command, you can do whatever you want with the user and the other
clients...

As I said, it's not pretty. But it seems to work 90% of the time. One
exemple where it doesn't work is if I close the browser with cmd+Q (on
OSX)... For me it's an "ok" solution, but a more straightforward way
to change the default timeout would be nice. Before using Ape, I was
using Java Socket + Flash. I was kind of used to the socket closing
instantly when the clients side was closed...

Wes Garland

unread,
Sep 16, 2011, 10:00:48 AM9/16/11
to ape-p...@googlegroups.com
Louis:

You can run ancient versions of IE on the Mac easily under WineBottler.

http://winebottler.kronenberg.org/

The emulation is so good, even the fonts suck.

Wes

PS: Thanks for sharing this post!

Nicolas Guibert

unread,
Sep 19, 2011, 9:27:01 AM9/19/11
to ape-p...@googlegroups.com
Thx for the info Louis.

this.core.quit results in deluser event to be fired. I don't use channels, but I guess there must be a way to deal with the deluser event to force the user to leave. Whether it is directly implemented in APE server-side or not (and you need to implement something).

Anyway, just my thoughts on this. As I said, I don't use channels.

Another thought to APE guys, I find a bit weird that deleting the user does not lead to him leaving all his channels. I believe that this should be automated as it is right into the core of APE features. Maybe I am missing something.

Nicolas.



2011/9/15 Louis Charette <charett...@gmail.com>
Reply all
Reply to author
Forward
0 new messages