Skip to content

Commit

Permalink
Added tags and channel presence
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Tsang committed Apr 26, 2016
1 parent e9043ca commit 3726baa
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.appengine.channel;

import com.google.appengine.api.channel.ChannelPresence;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChannelPresenceServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
//[START channel_presence]
ChannelService channelService = ChannelServiceFactory.getChannelService();
ChannelPresence presence = channelService.parsePresence(req);
//[END channel_presence]

System.out.println("Client ID: " + presence.clientId()
+ ", Connected: " + presence.isConnected());
resp.setStatus(HttpServletResponse.SC_OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public String getMessageString() {
return message.toString();
}

//[START send_updates]
public String getChannelKey(String user) {
return user + id;
return user + "." + id;
}

private void sendUpdateToUser(String user) {
Expand All @@ -131,6 +132,7 @@ public void sendUpdateToClients() {
sendUpdateToUser(userX);
sendUpdateToUser(userO);
}
//[END send_updates]

public void checkWin() {
final Pattern[] wins;
Expand All @@ -152,6 +154,7 @@ public void checkWin() {
}
}

//[START make_move]
public boolean makeMove(int position, String user) {
String currentMovePlayer;
char value;
Expand All @@ -175,4 +178,5 @@ public boolean makeMove(int position, String user) {

return false;
}
//[END make_move]
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
gameKey = game.getId();
}

//[START channel_service]
ChannelService channelService = ChannelServiceFactory.getChannelService();

// The 'Game' object exposes a method which creates a unique string based on the game's key
// and the user's id.
String token = channelService.createChannel(game.getChannelKey(userId));

//[END channel_service]

ofy.save().entity(game).now();

req.setAttribute("game_key", gameKey);
Expand Down
6 changes: 6 additions & 0 deletions appengine/channel/src/main/webapp/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<!--[START inbound_service]-->
<!-- Optional: Enable Inbound Service to get notified when a client connects to or disconnects from a channel -->
<inbound-services>
<service>channel_presence</service>
</inbound-services>
<!--[END inbound_service-->

</appengine-web-app>
6 changes: 6 additions & 0 deletions appengine/channel/src/main/webapp/WEB-INF/view/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
return state.userX == state.me ? 'X' : 'O';
}
//[START send_message]
sendMessage = function(path, opt_param) {
path += '?gamekey=' + state.game_key;
if (opt_param) {
Expand All @@ -163,12 +164,15 @@
xhr.open('POST', path, true);
xhr.send();
};
//[END send_message]
//[START move_in_square]
moveInSquare = function(id) {
if (isMyMove() && state.board[id] == ' ') {
sendMessage('/move', 'i=' + id);
}
}
//[END move_in_square]
highlightSquare = function(id) {
if (state.winner != "") {
Expand Down Expand Up @@ -205,6 +209,7 @@
}
openChannel = function() {
//[START open_channel]
var token = '<%= request.getAttribute("token") %>';
var channel = new goog.appengine.Channel(token);
var handler = {
Expand All @@ -216,6 +221,7 @@
var socket = channel.open(handler);
socket.onopen = onOpened;
socket.onmessage = onMessage;
//[END open_channel]
}
initialize = function() {
Expand Down
12 changes: 12 additions & 0 deletions appengine/channel/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet-name>MoveServlet</servlet-name>
<url-pattern>/move</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ChannelPresenceServlet</servlet-name>
<servlet-class>com.example.appengine.channel.ChannelPresenceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChannelPresenceServlet</servlet-name>
<url-pattern>/_ah/channel/connected/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ChannelPresenceServlet</servlet-name>
<url-pattern>/_ah/channel/disconnected/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>ObjectifyFilter</filter-name>
Expand Down

0 comments on commit 3726baa

Please sign in to comment.