AppEngine Channel API - Best way to check if a channel is still open (server side)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 07:24:00

Part 1: Solving your problem

See Part 2 below if you really need to track client connections, but I'm not sure from your question if what you're asking for will solve your problem.

Let me see if I can echo your problem back: you're writing a chat app, but it's for a site which isn't fully AJAX (in the way that, say, gmail is); the site contains page navigation where you may need to re-set up your channel after the user clicks on a link to another page. When the user navigates, the new page is rendered, and you want to avoid getting a new token at that point; you want to reuse the existing token and channel with the same client-id.

If that's correct, I have two alternate solutions, one easy but with not-great user experience, one trickier but with a much smoother end result.

  • Preserve the token in a cookie. When you re-render your page, just use the token from the cookie instead of calling channel.create_channel again. When the token expires you'll get an onerror callback just like if the user had stayed on the original page; at this point, call channel.create_channel again. The problem with this is that re-connection can be slow (up to 10 seconds or more in bad cases) because of the nature of Comet connections.

  • Wrap your entire site that's not chat-related in an iframe. Put your channel creation code and UI in the outer iframe. This way you don't have to re-connect every time the user navigates. This avoids the downtime on navigation. Note that orkut uses this technique, with floating divs, as a small amount of Firebug investigation will reveal.

Part 2: Your feature request

If it turns out I'm misunderstanding and you really do need to track client connections:

There's not a built-in way to check if a client is connected to a channel identified by a client-id now.

However, I am working right now on adding "presence" (in the chat sense) so that your app can register to get a post when a client connects to or disconnects from a channel created with a given client id. You also might be able to "probe" presence, to query whether a given client id is connected or not (still working on the details of this part).

Note that this won't be token-based, but rather client-id based.

I don't have a definite release date for this yet but as I said I'm actively working on it right now.

In the meantime, you could use a heartbeat HTTP request from your client back to your app that says, "hey, I'm still here" every minute or so. You'll need to have some sort of task that runs every, say, 2 minutes and marks any clients that haven't checked in as inactive, and you'll need to store this data someplace.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!