App Engine Python模块和频道服务 [英] App Engine Python Modules and channel service
问题描述
我在我的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
应用程序:整合
版本:1-0-0
运行时:python27
线程安全:true
builtins:
- deferred:在
库中:
- name:pycrypto
版本:2.6
handlers :
- url:/favicon\.ico
static_files:static / favicon.ico
上传:static / favicon\.ico
- url:/ admin /.+
脚本:src.default.main.app
登录名:admin
- url:/.*
脚本:src.default.main。 app
api.yaml
api_version:1
应用程序:整合
模块:api
版本:1-0-0
运行时:python27
线程安全:true
inbound_services:
- channel_presence
bui ltins:
- deferred:在
库中:
- 名称:pycrypto
版本:2.6
处理程序:
- url:/admin/.+
脚本:src.api.main.app
登录名:admin
- url:/.*
脚本:src .api.main.app
dispatch.yaml
应用程序:整合
发送:
- url:* / _ ah / channel / *
模块:api
注意:要清楚这一切在本地开发模式下工作。
api.main.app
app = webapp2.WSGIApplication(debug = True)
_routes = [
:
ChannelDisconnectedHandler.mapping(),
ChannelConnectHandler.mapping()
]
for self._routes:
app.router.add(r)
ChannelDisconnectHandler
CHANNEL_DISCONNECTED_UR L_PATTERN ='/ _ah / channel / disconnected /'
class ChannelDisconnectedHandler(RequestHandler):
@classmethod
def mapping(cls):
返回CHANNEL_DISCONNECTED_URL_PATTERN,cls
def post(self):
Channel Presence处理程序。将在客户端断开连接时调用。
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映射(cls):
返回CHANNEL_CONNECT_URL_PATTERN,cls
def post(self):
频道存在处理程序。将在客户端连接时调用。
channel_id = self.request.get('from')
logging.info(Channel Connect。Id:%s%channel_id)
$所以我的客户端(用javascript编写)发布到我的api模块并打开一个频道。
$ b $ b>
$ b 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错误。代码:+ e.code +,描述:+ e.description);
};
onClose = function(){
console.info(Close Channel );
};
onMessage =函数(msg){
console.info(Message Received:+ msg +,Data:+ msg.data) ;
};
这个回调函数是通过一个有效的令牌来实现的。在我的本地系统上,onOpened函数然后被调用,并且我从服务器接收消息。在生产onOpened从不被调用,我从来没有收到任何消息。/ _ah / channel / connected /也是从来没有打过电话。
频道服务是否不支持模块?对于我缺少的内容有任何想法?
解决方案根据Google企业支持(稍微修改其原始答案):
-
channel_presence
入站服务在 app.yaml
中启用。
inbound_services:
- channel_presence
在模块的yaml文件中启用这个入站服务(例如,在这个问题中 api.yaml
)将不会启用此服务。 / b>
-
以 * / _ ah
开头的网址路径不是可分派路径,无法路由 dispatch.yaml
。因此,必须在 app.yaml
中描述 channel_presence
URL路径处理程序。
处理程序:
- url:/ _ah / channel / connected /
script:mymodule.application
I am using App Engine Modules in my python project. (https://developers.google.com/appengine/docs/python/modules/#Python_Background_threads)
I am also using channels in m project: https://developers.google.com/appengine/docs/python/channel/
I want to direct the connected/disconnected post messages ('/_ah/channel/connected/', '/_ah/channel/disconnected/') to my api module. Right now I can't get them to show up in any module (default or 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
Note: Just to be clear this all works in dev mode locally.
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)
So my client (written in javascript) posts to my api module and opens a channel.
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);
};
This callback function is reached with a valid token. I create the socket successfully and complete this function as expected. On my local system the onOpened function is then called and I receive the messages from the server. In production onOpened is never called and I never receive any messages. The /_ah/channel/connected/ is also never called.
Is the Channel service not supported with modules? Any thoughts as to what I am missing?
解决方案 According to Google Enterprise Support (modified slightly from their raw answer):
channel_presence
inbound service must be enabled in app.yaml
.
inbound_services:
- channel_presence
Enabling this inbound service in module’s yaml file (e.g., api.yaml
in this question) won’t enable this service.
URL paths starting with */_ah
are not dispatchable paths and cannot be routed by dispatch.yaml
. Therefore, channel_presence
URL paths handlers have to be described in app.yaml
.
handlers:
- url: /_ah/channel/connected/
script: mymodule.application
这篇关于App Engine Python模块和频道服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!