Google App Engine 通道 API (Python/Django) 问题
嘿,大家。我正在尝试使用 App Engine Channel API(此处记录) 为我的应用程序启用推送更新。但是,我在设置初始化通信通道的机制时遇到了一些问题。
问题是,当我加载有问题的页面时,我得到了 onError 和 onClose 处理程序的调试打印,但没有得到 onOpen 处理程序的调试打印。没有其他事情发生。下面是使用 Google Chrome 开发者工具的 javascript 控制台输出:
资源解释为脚本,但以 MIME 类型 text/html 传输。 .....jsapi:-1
错误.....443088:88
关闭.....443088:80
这是我的 Django 模板中(大部分)javascript 代码的相关部分:
<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>
如果我理解正确的话,那么服务器端代码此时不相关,因为 onOpen 似乎还没有被调用。我知道我一定错过了一些简单的东西,但如果有人能帮忙解决这个问题,我将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您正在使用统一的通道令牌。服务器需要调用channel.create_channel来检索该客户端的令牌,然后将其传递给Channel对象的构造函数。
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.