Java 谷歌应用引擎频道API
我试图学习GAE的channel API(使用Java),但我不知道从哪里开始 我看了一遍,但出于简洁的目的,贴在那里的代码并不完整 由于我是新手,如果有完整的示例代码,这将非常有帮助 谢谢,Java 谷歌应用引擎频道API,java,google-app-engine,channel-api,Java,Google App Engine,Channel Api,我试图学习GAE的channel API(使用Java),但我不知道从哪里开始 我看了一遍,但出于简洁的目的,贴在那里的代码并不完整 由于我是新手,如果有完整的示例代码,这将非常有帮助 谢谢, Shrey您链接到的Channel API概述中的代码非常完整,只是有点杂乱无章。我承认,一旦你理解了,我觉得这比他们所说的要简单得多,但我很高兴他们在提供太多信息方面犯了错误 在没有外来信息的情况下给出完整的解决方案有点困难,因为您将如何使用Channel API有点依赖于现有应用程序的基础设施。出于这
Shrey您链接到的Channel API概述中的代码非常完整,只是有点杂乱无章。我承认,一旦你理解了,我觉得这比他们所说的要简单得多,但我很高兴他们在提供太多信息方面犯了错误 在没有外来信息的情况下给出完整的解决方案有点困难,因为您将如何使用Channel API有点依赖于现有应用程序的基础设施。出于这个原因,我试着对AppEngine文档提供的内容进行详细阐述,希望您能更好地理解。如果您有任何问题,评论将允许您提出具体问题 首先,一点词汇:
- 频道消息:您希望发送给客户端的消息(可能是您首先使用频道API的原因)
- 频道键:用户唯一的字符串,以及用户试图发送消息的范围
- 通道令牌:对任何客户端都是唯一的字符串。每2小时每个客户端1个通道令牌
- 频道服务:AppEngine服务器端类,提供创建频道和通过频道发送频道消息的方法
在服务器上,您需要执行以下操作:
ChannelService channelService = ChannelServiceFactory.getChannelService();
// The channelKey can be generated in any way that you want, as long as it remains
// unique to the user.
String channelKey = "xyz";
String token = channelService.createChannel(channelKey);
一旦您拥有了令牌,您只需要某种方法将其发送到客户端代码。您链接到的AppEngine文档通过从Javaservlet提供HTML并调用index.replaceAll(\\{\\\{token\\}\\},token)
来实现这一点
其工作原理是,他们将文本字符串{{token}}
放在JavaScript代码中(如下所示),因此无论{{token}}
出现在JavaScript代码中的什么地方,它都将被上面的channelService.createChannel(…)
调用生成的实际令牌所取代。请注意,您不需要将令牌注入到以这种方式提供服务的客户端代码中,但这是一个很好的起点,因为他们就是这样做的(并记录在案)
现在您已经将令牌注入到JavaScript中,您需要将带有通道令牌的代码获取到客户端。(请注意,如上所述,您还可以只向客户端获取令牌,并以这种方式创建通道)。他们拥有的代码是:
<body>
<script>
channel = new goog.appengine.Channel('{{ token }}');
socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
</script>
</body>
我仍然建议将它们分离成单独的函数,这样您就可以更容易地扩展它们来处理和解决问题。有关JavaScript API的更多详细信息,请参阅
您需要建立一种机制来获取要从客户端发送到服务器的数据。再说一次,你希望怎么做并不重要。AppEngine文档建议设置一个
XMLHttpRequest
来达到这个目的
sendMessage = function(path, opt_param) {
path += '?g=' + state.game_key;
if (opt_param) {
path += '&' + opt_param;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', path, true);
xhr.send();
};
这里,opt_param
只是一个可选参数字符串,格式为x=1&y=2&z=3
。这是他们为示例Tic-Tac-Toe应用程序构建的所有基础设施,对Channel API的功能并不重要;就像我说的,你可以随便打这个电话
path
是指向servlet(需要在web.xml文件中设置)的路径,该servlet应处理消息的发送和接收(请参见以下部分)
将消息从客户端发送到服务器后,需要一个servlet,它可以使用相同的通道密钥向所有客户端发送更新
ChannelService channelService = ChannelServiceFactory.getChannelService();
// This channelKey needs to be the same as the one in the first section above.
String channelKey = "xyz"
// This is what actually sends the message.
channelService.sendMessage(new ChannelMessage(channelKey, "Hello World!"));
上面的channelService.sendMessage(…)
调用实际上是发送消息的调用,因此您在上一节中定义的onMessage
函数可以接收消息
我希望这个答案是完整的(就这一点而言,是正确的),足以帮助您开始。他们放在文档中的大部分内容(这里还有我的代码)都可以复制和粘贴,只需稍加调整。我是StackOverflow新手,不确定这个问题是否仍然存在,但如果您仍在寻找使用Google的Channel API的完整Java示例,包括服务器端(Java)和客户端(Java)你可以在这里找到我写的详细描述: 它列出了从创建通道(客户机和服务器)、在通道(客户机和服务器)上发送消息到Java客户机可以用来与通道交互的简单框架的所有内容。我也很难理解谷歌的文档并理解它。我希望这些信息仍然是相关的和有用的:-) 完整的源代码和聊天示例可以在GitHub上找到: 下面是一些示例代码,希望对您有所帮助:-)
Java客户端通道创建(使用ChannelAPI框架:Jacc)
Java服务器端通道创建:
public class ChatChannelServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String channelKey = req.getParameter("c");
//Create a Channel using the 'channelKey' we received from the client
ChannelService channelService = ChannelServiceFactory.getChannelService();
String token = channelService.createChannel(channelKey);
//Send the client the 'token' + the 'channelKey' this way the client can start using the new channel
resp.setContentType("text/html");
StringBuffer sb = new StringBuffer();
sb.append("{ \"channelKey\":\"" + channelKey + "\",\"token\":\"" + token + "\"}");
resp.getWriter().write(sb.toString());
}
}
Java客户端消息发送(使用ChannelAPI框架:Jacc)
Java服务器端消息发送:
public class ChatServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String channelKey = req.getParameter("channelKey");
String message = req.getParameter("message");
//Send a message based on the 'channelKey' any channel with this key will receive the message
ChannelService channelService = ChannelServiceFactory.getChannelService();
channelService.sendMessage(new ChannelMessage(channelKey, message));
}
}
非常感谢:)我对此感到困惑。。replaceAll(“\\{\\{token\\\\}”,token),现在我可以开始了:)工作很好……),我如何多播消息。。?有API吗?这个答案应该得到某种奖励:)你是说一个来自Java的sendMessage调用将消息发送到多个客户端,每个客户端使用不同的令牌,共享通道密钥?这似乎与at的文档相矛盾,该文档指出“一次只有一个客户端可以使用给定的客户端ID连接到通道,因此应用程序不能使用Cl
public class ChatChannelServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String channelKey = req.getParameter("c");
//Create a Channel using the 'channelKey' we received from the client
ChannelService channelService = ChannelServiceFactory.getChannelService();
String token = channelService.createChannel(channelKey);
//Send the client the 'token' + the 'channelKey' this way the client can start using the new channel
resp.setContentType("text/html");
StringBuffer sb = new StringBuffer();
sb.append("{ \"channelKey\":\"" + channelKey + "\",\"token\":\"" + token + "\"}");
resp.getWriter().write(sb.toString());
}
}
/***
* Sends your message on the open channel
* @param message
*/
public void sendMessage(String message){
try {
channel.send(message, "/chat");
} catch (IOException e) {
System.out.println("Problem Sending the Message");
}
}
public class ChatServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String channelKey = req.getParameter("channelKey");
String message = req.getParameter("message");
//Send a message based on the 'channelKey' any channel with this key will receive the message
ChannelService channelService = ChannelServiceFactory.getChannelService();
channelService.sendMessage(new ChannelMessage(channelKey, message));
}
}