我在使用 Channel API 时遇到了困难,但仅限于生产环境。在开发服务器上,这是可行的,但在生产中,我的 javascript 中出现了 Invalid+token 错误,并且 Python 中出现了某种空白错误。
这是我的服务器端代码。 “channel_test”是我的 Controller ,“do_send_message”是一个延迟函数,“send_message”是一个带有 try- except block 的包装器。基本上它是创建 channel ,将 token 设置为 cookie,并调用延迟任务,直到计数器达到 100。每次运行任务时,它都会通过 channel 发送一条消息。
def channel_test(self, key):
if not users.is_current_user_admin(): return 403
client_id = self.user.email()+key
token = channel.create_channel(client_id, duration_minutes=10)
print 'token: %s' % token
self.response.set_cookie('token', urllib.quote(token))
deferred.defer(do_send_message, client_id, _queue='retry')
# channel communication
def send_message(client_id, msg):
"""
msg is a dict
"""
if client_id:
print 'client_id: ', client_id
try:
channel.send_message(client_id, json.dumps(msg))
print 'sent message %s' % msg
except Exception as e:
print 'something went wrong with msg, %s: %s, %s' % (msg, e, e.__str__())
else:
print 'not sending message; no client id'
def do_send_message(client_id, x=10):
time.sleep(1)
text = "here's a message! %s" % x
if x >= 100:
text = "Done!"
msg = {"text":text, "percent":x}
send_message(client_id, msg)
if x < 100:
deferred.defer(do_send_message, client_id, x+10, _queue='retry')
每次调用“send_message”都会失败,但“e”不会打印任何内容。这是日志中的输出:
something went wrong with msg, {'text': "here's a message! 10", 'percent': 10}: ,
此外,我的客户端 JavaScript 无法打开连接。它只是收到错误“无效+ token ”。这基本上从 cookie 中读取 token ,打开一个连接,每次传递消息时,它都会将该消息写入通知托盘,直到消息 ==“完成!”然后重新加载页面。但是,正如前面所说,它立即失败,并且 onError 打印“Object {description:“Invalid+token.”, code:“401”}'。
// globals
var channel, socket, hide;
var $msg = $('#channel-message');
var $bar = $('#notification-tray .progress-bar');
onMessage = function(obj){
var message = JSON.parse(obj.data);
if (message.percent !== undefined){
$bar.css('width', message.percent+'%');
}
switchMsg(message.text);
};
onOpen = function(){
$('#notification-tray').fadeIn();
$bar.css('width', '10%');
};
onClose = function(){
$('#notification-tray').fadeOut();
};
onError = function(err){
$('#notification-tray').fadeOut();
console.log(err);
};
function closeAndReload(){
socket.close();
$.removeCookie('token', {path:'/'});
$.removeCookie('hide', {path:'/'});
location.reload();
}
function switchMsg(msg){
$msg.fadeOut(function(){
$msg.html(msg);
$msg.fadeIn(function(){
if (msg == 'Done!'){
closeAndReload();
}
});
});
}
function initializeChannel(){
channel = new goog.appengine.Channel(token);
socket = channel.open();
socket.onmessage = onMessage;
socket.onopen = onOpen;
socket.onclose = onClose;
socket.onerror = onError;
}
$(function(){
token = $.cookie('token');
if (token !== undefined && token != ""){
initializeChannel();
}
});
这在我的开发服务器上完美运行,尽管我在生产中遇到了这个问题。
提前致谢。
最佳答案
好吧,这很令人沮丧,但我想我已经发现 token 有一个未记录的最大长度。我将 client_id 设置为
client_id = self.user.email()+key[0:10]
而不是
client_id = self.user.email()+key
现在可以了。当然,令人烦恼的是(1)它在开发服务器上运行而不是在生产服务器上运行,并且(2)错误信息不是很丰富。
关于javascript - channel API - 生产中的 token 无效,可在开发服务器上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798052/