I present to your attention the story of using the Channel API in the game of tic-tac-toe based on this article here:
Channel API Overview (Java) .

On Habré there
was already
an article about the Channel API, but there python and chat, and here there will be a game of tic-tac-toe and java. The source of the game
here , the author of the source is not me. If it doesn’t work for you then do as written in
Issue 3 .
As you know, Channel API allows you to establish a connection between a client (web page, javascript) and Google App Engine.
Here, knowledgeable people say that it works through XMPP at the moment, but in the future it will be possible to use WebSockets. For the game of tic-tac-toe, this means that the player will be able to get the result of his opponent’s move without using polling. You can enable the javascript library and create a channel as follows (on the client side):
<script src='/_ah/channel/jsapi'></script>
In this code, token is a unique identifier of the channel (further token). In this game, it is recorded by the server directly into the web page and the client immediately has it. The token is generated by the server using a method call.
String token = ChannelServiceFactory.getChannelService().createChannel(" ");
. The validity of the token is 2 hours. In this example, an identifier is passed to createChannel which is generated from the user id (
String userId = UserServiceFactory.getUserService().getCurrentUser().getUserId();
) and the key of the game, according to which the game object is stored in the permanent storage. At the entrance for each user, a game is immediately created, in which he can invite another player by giving him a link containing the game key. The createChannel parameter should be shorter than 64 bytes when presented in utf-8 (
Issue 3 ), in fact there is a fix because the source code from svn has problems with it.
')
This example uses 3 servlets. TicTacToeServlet does all the basic work by creating for the user a game, a channel, and writing everything down to the body of the web page. OpenedServlet responds to the client's request after the client has booted up creates a channel on his side and gives the client the current state of the game. The game in this case either has not started yet or if the client reloads the page during the game has a state different from the initial one. MoveServlet receives client requests for their turn and updates the game. That includes sending updates to players using the Channel API. Thus, the Channel API is used to update the game status for both participants.
Also of interest here is the use of JDO for storing games. Perhaps it would be better to use Memcache to store games. And how the playing field is made - canvas is not used.
The Channel API can also be used to check client connection / disconnection. To do this in appengine-web.xml need to add:
<inbound-services> <service>channel_presence</service> </inbound-services>
Then clients will be knocked to you by POST messages on / _ah / channel / connected / and / _ah / channel / disconnected / when connecting and disconnecting, respectively. You can check the connection on the server side like this:
ChannelServiceFactory.getChannelService(); ChannelPresence presence = channelService.parsePresence(req); String id = presence.clientId(); boolean connected = presence.isConnected();
What can be used by expanding the example and adding there the ability to select opponents from the list of connected players.
Token - the channel key is preferably kept secret. After 2 hours, it expires and onerror (), onclose () handlers are triggered on the client (see the channel creation code).
Of the minuses of this technology, it is worth noting the lack of embedded broadcasts. Those. General chat need to do yourself. Also, one web page can not create more than one channel.
Sample sources
here . To work with SDK version 1.7 I had to add
<threadsafe>true</threadsafe>
in appengine-web.xml.
It also describes how to create a chat on Google App Engine / java using the Channel API:
Tutorial: Code Lab Exercise 4: Channel API .