0

localhost で gae Web アプリを実行しています。goog.channel からトークンを正常に生成し、クライアントに送信しました。クライアントがトークンを受け入れることができ、接続を開こうとする場所。問題は、サーブレット クラスからメッセージを送信しているのに、クライアント側で何も起きていないことです。

以下は私のコードです:

サーバ側:

//for generating token 
 ChannelService channelService=ChannelServiceFactory.getChannelService();
                    token = channelService.createChannel(userid);
//for sending message
ChannelService channelService=ChannelServiceFactory.getChannelService();
            channelService.sendMessage(new ChannelMessage(userid, message));

    //in appengine-web.xml
     <inbound-services>
            <service>channel_presence</service>
      </inbound-services>

Javascript:

function getToken(){                        
        var xmlhttpreq=new XMLHttpRequest();            
        xmlhttpreq.open('GET',host+'/channelapi_token?q='+user,false);
        xmlhttpreq.send();
        xmlhttpreq.onreadystatechange=alert(xmlhttpreq.responseText);
        token=xmlhttpreq.responseText;
        setChannel();
}

function setChannel(){
        alert(token);//iam receiving right token here
        channel=new goog.appengine.Channel(token);
        socket=channel.open();
        socket.open=alert('socket opened');//this message alerts
        socket.onmessage=alert('socket onmessage');//this message alerts
        socket.onerror=alert('socket onerror');//this message alerts
        socket.onclose=alert('socket onclose');//this message alerts
}

channelservice からメッセージを送信する際に例外はありません。また、クライアント側はサーバーに get リクエストを繰り返し行っています。

http://localhost:8888/_ah/channel/dev?command=poll&channel=channel-h1yphg-vivems@gmail.com&client=connection-3

ここで何が間違っているのですか?前もって感謝します。

4

1 に答える 1

1

alert(...) を呼び出し、その戻り値をメッセージ ハンドラーに割り当てています。代わりに、これらのハンドラーに関数を割り当てる必要があります。

    socket.onopen = function() {
      alert('socket opened');
    };
    // etc
    // Note that message takes a parameter:
    socket.onmessage = function(evt) {
      alert('got message: ' + evt.data);
    };

次のようにすることもできます。

function onMessage(evt) {
  // do something
}

socket.onmessage = onMessage;

を割り当てていないことに注意してください。onMessage()これは onMessage を呼び出し、その戻り値を割り当てます。

于 2012-01-26T15:33:08.700 に答える