我按照几个类似的教程(看起来都很相似)使用Pusher为我的Rails应用程序实现实时消息传递。
我正在尝试的内容:
// Javascript on my clientside receiving html page
$(function(){
var pusher = new Pusher('xxxx-mykey-xxxx');
var myChannel = pusher.subscribe('survey-channel');
myChannel.bind('data-changed', function(data){
alert(data);
});
// Some useful debug msgs
pusher.connection.bind('connecting', function() {
alert('Connecting to Pusher...');
});
pusher.connection.bind('connected', function() {
alert('Connected to Pusher!');
});
pusher.connection.bind('failed', function() {
alert('Connection to Pusher failed :(');
});
myChannel.bind('subscription_error', function(status) {
alert('Pusher subscription_error');
});
});
在我的Rails应用程序中,我将其包含在控制器方法内:
Pusher['survey-channel'].trigger('data-changed', "Busy creating new Call")
render :text => "Pusher sent"
当该方法被调用时,除了呈现“Pusher sent”之外,没有其他内容发生。
在我的Chrome开发者工具控制台中,我得到以下内容:
(x) Uncaught TypeError: Cannot call method 'bind' of undefined calls:31
(3) WebSocket is closed before the connection is established. :3000:1
我可能漏掉了什么?请帮忙!
编辑 #1
我在客户端添加了这段代码:
Pusher.log = function(message) {
if (window.console && window.console.log) window.console.log(message);
};
然后它将以下内容记录到控制台:
Pusher : Connecting : ws://ws.pusherapp.com:80/app/e5d0f1ae2cab03dc3aa9?client=js&version=1.8.6 calls:44
Pusher : Event recd (event,data) : pusher:connection_established : {"socket_id":"14299.674628"} calls:44
Pusher : Event sent (channel,event,data) : : pusher:subscribe : {"channel":"survey-channel"} calls:44
编辑 #2
每当我发送“消息”时,即使我使用事件创建器发送它,它也不会出现在Pusher的调试控制台中。但真正奇怪的是,在统计信息页面上,它指示消息速率和连接数大于零,例如 2个连接等。
编辑 #3
我已经测试了Pusher的测试页面。(http://test.pusher.com/1.12) 它可以完美连接,但“Say Hello”功能没有任何作用。这让我想到可能是我的浏览器设置问题。
我当前在Win7 PC上运行最新版本的Chrome浏览器。
var pusher = new Pusher('mykey', { encrypted: true });
- Pangolin