python - channel API 存在未触发

标签 python google-app-engine channel-api

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

这是我的一些代码。

app.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

ma​​in.py

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>

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

这段代码有什么问题吗?

最佳答案

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

When the application receives a request, it tries to match each one in order until one matches, and then call the corresponding handler.

关于python - channel API 存在未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10152501/

相关文章:

php - 将数据库导入 Google Cloud SQL 错误 : "mysql_query Duplicate entry ' 1' for key ' PRIMARY"

python - 如何使用ndb游标翻到上一页?

google-app-engine - 连接到 channel 的客户端间歇性错误代码 400,描述 ""

google-app-engine - App Engine Python 模块和 channel 服务

python - 获取 URL 时如何删除 Google App Engine 默认 header ?

javascript - Chrome 扩展 - 实现 channel

python - 准确找到最小的特征值

python - 连接两个具有相同列的数据帧,但给我 ValueError : columns overlap but no suffix specified

python - 在 HTML 页面中查找单词的快速算法

python - 如何从python列表中获取零和非零值的索引范围?