0

まず、すばらしい Channel API を提供してくれた Moishe に感謝します。Google Channel API ドキュメントに記載されている簡単なシナリオを実行しています。私が直面している問題は、チャネルが開いた直後に閉じられることです。

/* Client Side */
public class Feed extends HttpServlet {


private static String feed=
   "<html>" +
   "<head>" +
   "<title>Login</title>" +
   "<script type=\"text/javascript\" src="/_ah/channel/jsapi\"></script>" +
   "</head>" +
   "<body>" + 
   "Feed" +
   "<script>" +
   "channel=new goog.appengine.Channel('{{ token }}');" +
   "socket=channel.open();" +
   "socket.onOpen=alert(\"channel opened");" +
   "socket.onMessage=alert(\"New Message\");" +
   "socket.onClose=alert(\"Socket Closed\");" +
   "socket.onError=alert(\"Error\");" +
   "</script>" +
   "</body>" +
   "</html>";"

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{

    ChannelService channelservice=ChannelServiceFactory.getChannelService();
    String token=channelservice.createChannel("sample");
    feed = feed.replaceAll("\\{\\{ token \\}\\}", token);
    res.setContentType("text/html");
    res.getWriter().write(feed);
 }

}

/* Server Side*/

public class QuestAsk extends HttpServlet{

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    {
        ChannelService channelService=ChannelServiceFactory.getChannelService();
        channelService.sendMessage(new ChannelMessage("sample","sample message"));
    }
 }

Feed に GET リクエストを発行します。

それは、チャネルが開いているのに続いて、未定義のメッセージが受信された -> チャネルが閉じられた -> チャネルエラーが表示されるときです。

明らかに、私はここで何かを見落としています。誰かがそれを指摘できれば非常に感謝します。助けてくれてありがとう。

よろしくお願いします

JR

4

1 に答える 1

1

私が見る2つの問題があります:

まず、onopen などには大文字がないため、ソケットに間違った値を割り当てています。socket.onopen、socket.onmessage、socket.onclose、socket.onerror を使用する必要があります。

次に、alert() への呼び出しの結果を socket.onOpen などに割り当てます。代わりに、これらの関数に関数を割り当てたいので、次のようにする必要があります。

socket.onopen = function() {alert('Channel opened.');};
// etc

これでコードが修正されると思います。

于 2012-04-02T12:43:37.583 に答える