GWT & channel API

微笑、不失礼 提交于 2019-12-11 07:38:15

问题


Anyone have experience with appengine channel api with GWT? I have been following the BRIEF "How-to" provided with the googlecode . My question is how do we get the channelKey(token variable in the code below) in GWT? I am assuming you must use an RPC to get the channelKey from the server for each session. Is this correct? I was hoping you could just use the channelId, but that does not appear to be the case. Also best answer will be award to anyone who can provide a working example code for GWT +channel api other that the dance-dance-robot example. I have looked long and hard for sample code or tutorial and found nothing.

The following code executes and displays the onError message. I am assuming "token" is the channelKey that is generated by the server code. Is this correct?

GWT Client Code:

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 Server code:

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());
    }

}


回答1:


No. Token in this case is generated by you and should be unique identifier of this channel. You are also responsible for sharing this token between users.

For example, if you are creating chat room, you have to create token related to chat room and notify users about chat room (which has a related token).



来源:https://stackoverflow.com/questions/7289519/gwt-channel-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!