Pusherapp/Websocket not connecting
I'm trying to use pusherapp here but chromium's console just gives me "WebSocket is closed before the connection is established." My JS is below, any ideas as to what might be the problem?
<script type='text/javascript'>
$(document).ready(function(){
var pusher_key = "<开发者_开发百科;%= Pusher.key %>";
var pusher_channel = "thirsty-<%= Rails.env %>";
var pusher = new Pusher(pusher_key, pusher_channel);
pusher.bind('push_comment', function(content) {
comment_html = '<li><p>' + content + '</p></li>'
$(comment_html).prependTo('#comments');
});
});
</script>
The question is asking the reason for the following error:
WebSocket is closed before the connection is established.
This is actually a generic WebSocket error which is logged by the browser when an attempt is made to close the WebSocket connection (by calling webSocketInstance.close()
) before the connection has even been established.
The error is triggered by the Pusher JavaScript library try to close a connection, but is caused by bad network or browser conditions (e.g. online/offline reporting). All all cases (that I'm aware of) the Pusher JavaScript library will retry connecting until those connections are resolved (e.g. the Internet connection is restored).
More information and an example in the following answer:
What does "WebSocket is closed before the connection is established" mean?
A couple of points on the code above:
- the
Pusher
constructor has a first parameter which is the application key. The second parameter is a map of key value options. So, you shouldn't be passing in a channel name as the second parameter. More info on thePusher
constructor here - The
pusher.bind
call is actually deprecated (docs being updated now). For channel events you should bind directly on the channel usingchannel.bind
and for connection events you should bind on thePusher.connection
object.
- It could happen if the internet connection is too slow or disconnected.
- Cross check the pusher Credentials.
- It you have cluster in pusher channel app make sure you write it in you code.
精彩评论