问题描述
我试图让渠道API工作。这是我到目前为止:
在视图中:
def channel_test(channel_token):
tries = 1
logging.info('starting channel_test')
(尝试):
message ='这是消息编号:'+ str(尝试)
channel.send_message(channel_token,消息)
logging.info('刚刚发送:'消息)
logging.info(channel_token)
def viewfunc():
channel_token = channel.create_channel('aosasdf123')
deferred.defer(channel_test,channel_token,_countdown = 10)
return render_template(' Main / cycle.html',form = form,channel_token = channel_token)
/ p>
< script type =text / javascriptcharset =utf-8>
函数tell_user(消息){
$('#CycleChannelMessages')。append(message +'< br />');
function onOpened(){
console.log('onOpened');
var connected = true;
tell_user('准备接收消息');
tell_user('{{channel_token}}');
函数onMessage(msg_obj){
console.log('onMessage');
tell_user('something');
// tell_user(msg_obj.data);
$ b函数onError(obj){
console.log('onError');
函数onClose(obj){
console.log('onClose');
}
var channel = new goog.appengine.Channel('{{channel_token}}');
var socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
< / script>
但我唯一得到的结果是从onOpen:
准备发送消息
channel-1788270053 -aosasdf123
而在控制台中,我只看到:
onOpened
所以没有运行其他函数。来自appengine启动器的日志清楚地表明延迟的函数正在运行,并且不会造成错误或警告。
现在我做错了什么,因为没有任何显示在前端。
这是在开发服务器顺便说一句。我还没有尝试过它在生产。
框架是烧瓶,如果有什么区别。
client_id
传递给 send_message
,而不是 channel_token
。所以你的代码应该是这样的:$ b $ $ $ $ $ $ $ $ $ code $ channel.send_message('aosasdf123',message)
放置channel_token客户端以打开通道,并在服务器端保留client_id秘密,以便通过通道将消息传输到该客户端。
I am trying to get the channel api working.This is what I have so far:
in the view:
def channel_test(channel_token):
tries = 1
logging.info('starting channel_test')
for attempt in range(tries):
message = 'this is message number: ' + str(attempt)
channel.send_message(channel_token, message)
logging.info('just sent: ' + message)
logging.info(channel_token)
def viewfunc():
channel_token = channel.create_channel('aosasdf123')
deferred.defer(channel_test, channel_token, _countdown=10)
return render_template('Main/cycle.html', form=form, channel_token=channel_token)
and in my template:
<script type="text/javascript" charset="utf-8">
function tell_user(message) {
$('#CycleChannelMessages').append(message + '<br />');
}
function onOpened() {
console.log('onOpened');
var connected = true;
tell_user('ready to take messages');
tell_user('{{ channel_token }}');
}
function onMessage(msg_obj) {
console.log('onMessage');
tell_user('something');
// tell_user(msg_obj.data);
}
function onError(obj) {
console.log('onError');
}
function onClose(obj) {
console.log('onClose');
}
var channel = new goog.appengine.Channel('{{ channel_token }}');
var socket = channel.open();
socket.onopen = onOpened;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
</script>
But the only output I get is from onOpen:
ready to take messages
channel-1788270053-aosasdf123
And in the console I only see:
onOpened
So no other function has been run. The logs from the appengine launcher, clearly shows that the deferred function is being run and it is causing no errors or warnings.
Now what did I do wrong since nothing is showing up at the front-end.This is on the dev-server BTW. I have not tried it in production yet.
Framework is Flask if that makes any difference.
You pass the client_id
to send_message
not the channel_token
. So your code should be:
channel.send_message('aosasdf123', message)
You place the channel_token client-side for opening the channel, and keep the client_id secret on the server-side for transmitting messages to that client via the channel.
这篇关于appengine频道没有消息到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!