我在我的 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)中
应用程序.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
调度.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)
通道连接处理程序
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 服务吗?关于我缺少什么的任何想法?