0

我在 localhost 上运行一个 gae Web 应用程序。我已成功从 goog.channel 生成令牌并将其发送给客户端。客户端能够接受令牌并尝试打开连接的位置。问题是,我正在从我的 servlet 类发送一条消息,而客户端没有发生任何事情。

以下是我的代码:

服务器端:

//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 发送消息时没有异常。客户端也在反复向我的服务器发出获取请求:

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;

Note that you're not assigning onMessage(), which will call onMessage and assign its return value.

于 2012-01-26T15:33:08.700 回答