任何人都有使用 GWT 的 appengine channel api 的经验吗?我一直在关注googlecode提供的简要“操作方法” 。我的问题是我们如何在 GWT 中获取 channelKey(下面代码中的令牌变量)?我假设您必须使用 RPC 从服务器获取每个会话的 channelKey。这个对吗?我希望您可以只使用 channelId,但事实并非如此。最好的答案也将奖励给任何可以为 GWT +channel api 提供工作示例代码的人,而不是 dance-dance-robot 示例。我一直在努力寻找示例代码或教程,但一无所获。
以下代码执行并显示 onError 消息。我假设“令牌”是由服务器代码生成的 channelKey。这个对吗?
GWT 客户端代码:
ChannelFactory.createChannel(token, new ChannelCreatedCallback() {
@Override
public void onChannelCreated(Channel channel) {
channel.open(new SocketListener() {
@Override
public void onOpen() {
Window.alert("Channel opened!");
}
@Override
public void onMessage(String message) {
Window.alert("Received: " + message);
}
@Override
public void onError(SocketError error) {
Window.alert("Error: " + error.getDescription());
}
@Override
public void onClose() {
Window.alert("Channel closed!");
}
});
} });
Appengine 服务器代码:
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelServiceFactory;
@SuppressWarnings("serial")
public class SendChannelMsg extends HttpServlet {
private final String CHANNELNAME = "test";
private static String channelKey;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
if (channelKey == null) {
channelKey = ChannelServiceFactory.getChannelService()
.createChannel(CHANNELNAME);
}
String ret = "";
String command = req.getParameter("command");
if (command.equals("join")) {
ret = channelKey;
} else if (command.equals("send")) {
try{
ChannelServiceFactory.getChannelService()
.sendMessage(
new ChannelMessage(channelKey, req
.getParameter("message")));
} catch(Exception e){
resp.getWriter().println("error "+e.getMessage());
}
}
resp.getOutputStream().write(ret.getBytes());
}
}