36

我正在尝试使用 Channel API 中的 Presence 来处理断开/连接状态。

这是我的一些代码。

应用程序.yaml

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css))

- url: .*
  script: main.py

inbound_services:
- channel_presence

主文件

class onConnect(webapp.RequestHandler):
  def post(self):
    for user in users:
      users = User.all().fetch(1000)
      client = client_id = self.request.get('from')
      channel.send_message(user.channel,' connected');

class onDisconnect(webapp.RequestHandler):
  def post(self):
    Mainpage()
    for user in users:
      users = User.all().fetch(1000)
      client = client_id = self.request.get('from')
      channel.send_message(user.channel, ' disconnected');

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                     ('/_ah/channel/connected/',onConnect),
                                     ('/_ah/channel/disconnected/',onDisconnect),
                                     ('/chat',handleChat)],
                                     debug=True)

Javascript

<script>
        openChannel = function(){
            var token = '{{ token }}';
            var channel = new goog.appengine.Channel(token);
            var handler = {
                  'onopen': onOpened,
                  'onmessage': onMessage,
                  'onerror': function() {},
                  'onclose': function() {}
                };
            var socket = channel.open(handler);
            socket.onopen = onOpened;
            socket.onmessage = onMessage;

            var chat = document.getElementById('chatinput');
                chat.onkeyup = function(e){
                    if(e.keyCode == 13){
                        sendChat(this.value);
                        this.value = '';
                    }
                }
        }

        sendMessage = function(path, opt_param) {
            if (opt_param) {
                path += '?' + opt_param;
            }
            var xhr = new XMLHttpRequest();
            xhr.open('POST', path, true);
            xhr.send();
        };

        onOpened = function(){
            console.log('Channel Opened');  
            var chatlog = document.getElementById('chatlog');
            var msg = document.createElement('div');
                msg.innerHTML = 'Channel Opened';
                chatlog.appendChild(msg);
            sendMessage('/chat','m='+'A User Joined.');
        }

        onMessage = function(m){
            console.log('Message Recieved');
            var chatlog = document.getElementById('chatlog');
            var msg = document.createElement('div');
            var d = new Date();
                msg.innerHTML = d.toLocaleTimeString() + ': ' + m.data;
                chatlog.appendChild(msg);
        }

        sendChat = function(msg){
            console.log(msg);
            sendMessage('/chat','m='+msg);
        }

        openChannel();
    </script>

使用此代码,connnect并且disconnect不会在用户关闭浏览器或其他任何内容时触发。

这段代码有什么问题吗?

4

1 回答 1

1

是的,路线列表是错误的。放在('/', MainPage)路线列表的末尾。来自webapp2 URI 路由指南

当应用程序收到请求时,它会尝试依次匹配每一个,直到匹配到一个,然后调用相应的处理程序。

于 2015-08-27T11:46:06.253 回答