我有一個小片段引發「現有訂閱頻道」異常,即使我只調用一次訂閱方法。「現有訂閱頻道」,而只訂閱一次
這可以通過移動「state_change」處理程序外的訂閱請求來避免,但我想知道什麼可能會導致這種情況?也許Pusher庫中存在一個錯誤?
<!doctype html>
<html>
<body>
<h1>Pusher subscribe testcase</h1>
<p>Tip: check your console</p>
<script src="https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js"></script>
<script>
var pusher, channel;
pusher = new Pusher('xxxxxxxxxxxxxxxxx');
pusher.connection.bind('state_change', function(change){
if(change.current === 'connected'){
console.log('connected');
channel = pusher.subscribe('test-channel');
channel.bind('pusher:subscription_succeeded', function() {
console.log('subscribed');
});
}
})
</script>
</body>
</html>
這導致:
connected
subscribed
Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Existing subscription to channel test-channel"}}}
「你不需要綁定到state_change事件,以訂閱頻道「 - 這是正確的,但真的只是偶然遇到這種行爲:) 轉發此Pusher的問題跟蹤器https://github.com/pusher/pusher-js/issues/45 – dirkbonhomme