1

任何人都有使用 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());
    }

}

4

1 回答 1

0

不,在这种情况下,令牌是由您生成的,应该是该频道的唯一标识符。您还负责在用户之间共享此令牌。

例如,如果您正在创建聊天室,则必须创建与聊天室相关的令牌并通知用户聊天室(具有相关令牌)。

于 2011-09-03T08:33:23.003 回答