App Engine Python模块和频道服务

Dav*_*ard 10 google-app-engine module python-2.7 channel-api gae-module

我在我的python项目中使用App Engine模块.(https://developers.google.com/appengine/docs/python/modules/#Python_Background_threads)

我也在m项目中使用频道:https://developers.google.com/appengine/docs/python/channel/

我想将连接/断开的帖子消息('/ _ah/channel/connected /','/ _ah/channel/disconnected /')指向我的api模块.现在我无法让它们显示在任何模块中(默认或api)

的app.yaml

    api_version: 1
    application: integrate
    version: 1-0-0
    runtime: python27
    threadsafe: true

    builtins:
      - deferred: on

    libraries:
      - name: pycrypto
      version: "2.6"

    handlers:
      - url: /favicon\.ico
      static_files: static/favicon.ico
      upload: static/favicon\.ico

      - url: /admin/.+
      script: src.default.main.app
      login: admin

      - url: /.*
      script: src.default.main.app
Run Code Online (Sandbox Code Playgroud)

api.yaml

    api_version: 1
    application: integrate
    module: api
    version: 1-0-0
    runtime: python27
    threadsafe: true

    inbound_services:
      - channel_presence

    builtins:
      - deferred: on

    libraries:
      - name: pycrypto
      version: "2.6"

    handlers:
      - url: /admin/.+
      script: src.api.main.app
      login: admin

      - url: /.*
      script: src.api.main.app
Run Code Online (Sandbox Code Playgroud)

dispatch.yaml

    application: integrate

    dispatch:
       - url: "*/_ah/channel/*"
       module: api
Run Code Online (Sandbox Code Playgroud)

注意:要清楚这一切都在本地开发模式下工作.

api.main.app

    app = webapp2.WSGIApplication(debug=True)
    _routes = [
        :
        ChannelDisconnectedHandler.mapping(),
        ChannelConnectHandler.mapping()
    ]

    for r in self._routes:
        app.router.add(r)
Run Code Online (Sandbox Code Playgroud)

ChannelDisconnectHandler

    CHANNEL_DISCONNECTED_URL_PATTERN = '/_ah/channel/disconnected/'


    class ChannelDisconnectedHandler(RequestHandler):

        @classmethod
        def mapping(cls):
            return CHANNEL_DISCONNECTED_URL_PATTERN, cls

        def post(self):
            """
            Channel Presence handler. Will be called when a client disconnects.
            """
            channel_id = self.request.get('from')
            logging.info("Channel Disconnect. Id: %s" % channel_id)
Run Code Online (Sandbox Code Playgroud)

ChannelConnectHandler

    CHANNEL_CONNECT_URL_PATTERN = '/_ah/channel/connected/'

    class ChannelConnectHandler(RequestHandler):

        @classmethod
        def mapping(cls):
            return CHANNEL_CONNECT_URL_PATTERN, cls

        def post(self):
            """
            Channel Presence handler. Will be called when a client connects.
            """
            channel_id = self.request.get('from')
            logging.info("Channel Connect. Id: %s" % channel_id)
Run Code Online (Sandbox Code Playgroud)

所以我的客户端(用javascript编写)发布到我的api模块并打开一个频道.

    var open_channel = function(tokenResponse) {
        console.log("Open Channel. token Response: " + tokenResponse)
        token = tokenResponse.token;
        var channel = new goog.appengine.Channel(token);
        if (socket != null) {
            socket.close();
        }
        socket = channel.open();
        socket.onopen = onOpened;
        socket.onmessage = onMessage;
        socket.onerror = onError;
        socket.onclose = onClose;
    };

    onOpened = function() {
        console.info("Channel API Connection is open.");
    };

    onError = function(e) {
        console.info("CHANNEL Error. Code: " + e.code + ", Description: " + e.description);
    };

    onClose = function() {
        console.info("Close Channel");
    };

    onMessage = function(msg) {
       console.info("Message Received: " + msg + ", Data: " + msg.data);
    };
Run Code Online (Sandbox Code Playgroud)

使用有效令牌到达此回调函数.我成功创建了套接字并按预期完成了此功能.在我的本地系统上,然后调用onOpened函数,我从服务器接收消息.在生产中onOpened永远不会被调用,我从未收到任何消息./ _ah/channel/connected /也从不被调用.

模块不支持Channel服务吗?有什么想法,我错过了什么?

Emi*_*Sit 6

根据谷歌企业支持(从他们的原始答案略有修改):

  1. channel_presence必须启用入站服务app.yaml.

    inbound_services:
    - channel_presence
    
    Run Code Online (Sandbox Code Playgroud)

    在模块的yaml文件中启用此入站服务(例如,api.yaml在此问题中)将不启用此服务.

  2. 以...开头的URL路径*/_ah不是可分派路径,无法路由dispatch.yaml.因此,channel_presence必须描述URL路径处理程序app.yaml.

    handlers:
    - url: /_ah/channel/connected/
      script: mymodule.application
    
    Run Code Online (Sandbox Code Playgroud)


Chr*_*rez 0

您必须为连接和断开 URL 声明一个 hadler 路由。

处理程序路由main.py

application = webapp2.WSGIApplication([
    ...
    # Define a URL routing for /_ah/channel/connected/
    webapp2.Route(r'/_ah/channel/connected/',
                  handler=ChannelConnectedHandler,
                  name='channel_connected')

], debug=True, config=webapp2_config)


# Implement class handler of /_ah/channel/connected/
class ChannelConnectedHandler(webapp2.RequestHandler):
    def post(self):
        client_id = self.request.get('from')
        logging.info('client %s has connected!' % client_id)
        ...
Run Code Online (Sandbox Code Playgroud)