通道api中的消息失败

时间:2012-01-26 10:48:41

标签: google-app-engine channel-api

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

  

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

这里发生了什么错? 提前谢谢。

1 个答案:

答案 0 :(得分: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并分配其返回值。