Problem with Google App Engine Channel API (Python/Django)
-
25-09-2019 - |
문제
Hey, everybody. I'm trying to use the App Engine Channel API (documented here) to enable push updates for my application. However, I'm running into some problems setting up the mechanism for initializing a communication channel.
The problem is that, when I load the page in question, I get debug prints for the onError and onClose handlers, but I don't get a debug print for the onOpen handler. Nothing else happens. Below is the javascript console output using Google Chrome Developer Tools:
Resource interpreted as script but transferred with MIME type text/html. ..... jsapi:-1
onError ..... 443088:88
onClose ..... 443088:80
And here is the relevant section of (mostly) javascript code from my Django template:
<script type="text/javascript">
onOpen = function() {
console.debug('onOpen');
var xhrArgs = {
url: '/channel/connect/',
headers: { 'Content-Type': 'application/json' },
postData: dojo.toJson({ 'channel_token': '{{ channel_token }}' }),
handleAs: 'text',
load: function(response) {
alert('success');
},
error: function(error) {
alert('failure: ' + error);
}
};
var deferred = dojo.xhrPost(xhrArgs);
};
onClose = function() {
console.debug('onClose');
};
onMessage = function(msg) {
console.debug('onMessage');
};
onError = function() {
console.debug('onError');
};
openChannel = function() {
var channel = new goog.appengine.Channel('{{ channel_token }}');
var handler = {
'onopen': onOpen,
'onclose': onClose,
'onmessage': onMessage,
'onerror': onError
};
var socket = channel.open(handler);
socket.onopen = onOpen;
socket.onmessage = onMessage;
};
setTimeout(openChannel, 100);
</script>
If I understand this correctly, then the server-side code isn't relevant at this point, since onOpen doesn't appear to even be called yet. I know I must be missing something simple, but if anybody could help out with this, I would greatly appreciate it!
해결책
It looks to me like you're using an unitialized channel token. The server needs to call channel.create_channel to retrieve a token for this client, that you'll then pass to the Channel object's constructor.