App Engine Python模块和频道服务

时间:2013-10-03 15:23:41

标签: google-app-engine python-2.7 module 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

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

dispatch.yaml

    application: integrate

    dispatch:
       - url: "*/_ah/channel/*"
       module: api

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

api.main.app

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

    for r in self._routes:
        app.router.add(r)

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)

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)

所以我的客户端(用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);
    };

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

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

3 个答案:

答案 0 :(得分:6)

根据谷歌企业支持(稍微修改一下原来的答案):

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

    inbound_services:
    - channel_presence
    

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

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

    handlers:
    - url: /_ah/channel/connected/
      script: mymodule.application
    

答案 1 :(得分:0)

您必须为连接和断开URL声明处理程序路由。

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

答案 2 :(得分:0)

我也遇到了在模块中使用Channel API的问题,我尝试使用与Emil提到的类似技巧来解决这些问题,方法是将请求重定向到模块。

这是一个稍微复杂的设置,因为我实际上有3个模块,其中2个使用Channel API,其中一个是' frontend'。像这样:

  • 模块前端(默认)
  • 模块serviceA(使用channel api 1)
  • 模块serviceB(使用channel api 2)

我希望能够收听"通知"来自前端的两个独立服务。

我设法解决这个问题(在开发中)的方法是在前端添加重定向,读取我在每个服务上加上前缀的标记并重定向到每个服务。

"太好了,它有效!"我想,但是当我尝试部署到app引擎时,我意识到还有更多,因为Channel API内部使用的talkgadget端点似乎期望某个源应用程序,因此不允许跨域通信。

所以我最终使用了多个项目而不是模块,并且放置了一个HTML iframe" postMessage bridge"解决跨域问题。并且很高兴它的效果非常好,作为副作用,我获得了两倍的免费"要使用的渠道。

我在这里找到了与此相关的问题,您可能有兴趣跟踪:https://code.google.com/p/googleappengine/issues/detail?id=10293