我在我的Python項目中使用App Engine的模塊。 (https://developers.google.com/appengine/docs/python/modules/#Python_Background_threads)App Engine的Python模塊和通道服務
我也使用在米項目渠道:https://developers.google.com/appengine/docs/python/channel/
欲直接連接/斷開後的消息( '/ _ah /信道/連接/',「/ _ah /信道/斷開/ ')到我的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 /也不會被調用。
不與模塊支持的頻道服務?有什麼想法,我失蹤了?
謝謝回答,但恐怕不是這裏的問題。我沒有在這裏發佈代碼,但我確實添加了路線。如果我沒有,它將無法在開發模式下工作。 –
我添加了代碼來向你展示我的意思。 –