开发者

GWT & channel API

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) {
    W开发者_JAVA百科indow.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());
    }

}


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜