问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/6589559/appengine-channel-no-messages-arrive