Skip to content

Commit 9db28b6

Browse files
author
Ray Tsang
committed
Added tags and channel presence
1 parent e9043ca commit 9db28b6

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine.channel;
18+
19+
import com.google.appengine.api.channel.ChannelPresence;
20+
import com.google.appengine.api.channel.ChannelService;
21+
import com.google.appengine.api.channel.ChannelServiceFactory;
22+
23+
import javax.servlet.http.HttpServlet;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
import java.io.IOException;
27+
28+
public class ChannelPresenceServlet extends HttpServlet {
29+
@Override
30+
public void doPost(HttpServletRequest req, HttpServletResponse resp)
31+
throws IOException {
32+
//[START channel_presence]
33+
ChannelService channelService = ChannelServiceFactory.getChannelService();
34+
ChannelPresence presence = channelService.parsePresence(req);
35+
//[END channel_presence]
36+
37+
System.out.println("Client ID: " + presence.clientId() + ", Connected: " + presence.isConnected());
38+
resp.setStatus(HttpServletResponse.SC_OK);
39+
}
40+
}

appengine/channel/src/main/java/com/example/appengine/channel/Game.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ public String getMessageString() {
115115
return message.toString();
116116
}
117117

118+
//[START send_updates]
118119
public String getChannelKey(String user) {
119-
return user + id;
120+
return user + "." + id;
120121
}
121122

122123
private void sendUpdateToUser(String user) {
@@ -131,6 +132,7 @@ public void sendUpdateToClients() {
131132
sendUpdateToUser(userX);
132133
sendUpdateToUser(userO);
133134
}
135+
//[END send_updates]
134136

135137
public void checkWin() {
136138
final Pattern[] wins;
@@ -152,6 +154,7 @@ public void checkWin() {
152154
}
153155
}
154156

157+
//[START make_move]
155158
public boolean makeMove(int position, String user) {
156159
String currentMovePlayer;
157160
char value;
@@ -175,4 +178,5 @@ public boolean makeMove(int position, String user) {
175178

176179
return false;
177180
}
181+
//[END make_move]
178182
}

appengine/channel/src/main/java/com/example/appengine/channel/TicTacToeServlet.java

+6
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
8282
gameKey = game.getId();
8383
}
8484

85+
//[START channel_service]
8586
ChannelService channelService = ChannelServiceFactory.getChannelService();
87+
88+
// The 'Game' object exposes a method which creates a unique string based on the game's key
89+
// and the user's id.
8690
String token = channelService.createChannel(game.getChannelKey(userId));
8791

92+
//[END channel_service]
93+
8894
ofy.save().entity(game).now();
8995

9096
req.setAttribute("game_key", gameKey);

appengine/channel/src/main/webapp/WEB-INF/appengine-web.xml

+6
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@
2323
<system-properties>
2424
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
2525
</system-properties>
26+
<!--[START inbound_service]-->
27+
<!-- Optional: Enable Inbound Service to get notified when a client connects to or disconnects from a channel -->
28+
<inbound-services>
29+
<service>channel_presence</service>
30+
</inbound-services>
31+
<!--[END inbound_service-->
2632

2733
</appengine-web-app>

appengine/channel/src/main/webapp/WEB-INF/view/index.jsp

+6
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
return state.userX == state.me ? 'X' : 'O';
155155
}
156156
157+
//[START send_message]
157158
sendMessage = function(path, opt_param) {
158159
path += '?gamekey=' + state.game_key;
159160
if (opt_param) {
@@ -163,12 +164,15 @@
163164
xhr.open('POST', path, true);
164165
xhr.send();
165166
};
167+
//[END send_message]
166168
169+
//[START move_in_square]
167170
moveInSquare = function(id) {
168171
if (isMyMove() && state.board[id] == ' ') {
169172
sendMessage('/move', 'i=' + id);
170173
}
171174
}
175+
//[END move_in_square]
172176
173177
highlightSquare = function(id) {
174178
if (state.winner != "") {
@@ -205,6 +209,7 @@
205209
}
206210
207211
openChannel = function() {
212+
//[START open_channel]
208213
var token = '<%= request.getAttribute("token") %>';
209214
var channel = new goog.appengine.Channel(token);
210215
var handler = {
@@ -216,6 +221,7 @@
216221
var socket = channel.open(handler);
217222
socket.onopen = onOpened;
218223
socket.onmessage = onMessage;
224+
//[END open_channel]
219225
}
220226
221227
initialize = function() {

appengine/channel/src/main/webapp/WEB-INF/web.xml

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
4242
<servlet-name>MoveServlet</servlet-name>
4343
<url-pattern>/move</url-pattern>
4444
</servlet-mapping>
45+
<servlet>
46+
<servlet-name>ChannelPresenceServlet</servlet-name>
47+
<servlet-class>com.example.appengine.channel.ChannelPresenceServlet</servlet-class>
48+
</servlet>
49+
<servlet-mapping>
50+
<servlet-name>ChannelPresenceServlet</servlet-name>
51+
<url-pattern>/_ah/channel/connected/</url-pattern>
52+
</servlet-mapping>
53+
<servlet-mapping>
54+
<servlet-name>ChannelPresenceServlet</servlet-name>
55+
<url-pattern>/_ah/channel/disconnected/</url-pattern>
56+
</servlet-mapping>
4557

4658
<filter>
4759
<filter-name>ObjectifyFilter</filter-name>

0 commit comments

Comments
 (0)