admin管理员组文章数量:1419202
I'm new to using Pusher
with my Rails
app.
Everything is working fine and i'm now able to subscribe to channels and receive JSON
message.
But i want to make an button where i can unsubscribe from a specific channel.
I have tried this
<button onclick="unsubscribe_channcel('test_channel')">Unsubscribe</button>
<script>
function unsubscribe_channcel(channelName) {
var pusher = new Pusher('APP KEY');
pusher.unsubscribe(channelName);
};
</script>
But it do not work. If i look in the debug console when i press the button all that happens is that it make a new connection and i keep receiving messages from the "test_channel".
I'm new to using Pusher
with my Rails
app.
Everything is working fine and i'm now able to subscribe to channels and receive JSON
message.
But i want to make an button where i can unsubscribe from a specific channel.
I have tried this
<button onclick="unsubscribe_channcel('test_channel')">Unsubscribe</button>
<script>
function unsubscribe_channcel(channelName) {
var pusher = new Pusher('APP KEY');
pusher.unsubscribe(channelName);
};
</script>
But it do not work. If i look in the debug console when i press the button all that happens is that it make a new connection and i keep receiving messages from the "test_channel".
Share Improve this question edited Nov 30, 2014 at 16:27 niiicolai asked Nov 30, 2014 at 15:44 niiicolainiiicolai 732 silver badges12 bronze badges 01 Answer
Reset to default 3You need to unsubscribe to the channel on the same instance of the Pusher
object that you subscribed to the channel on e.g.
<button onclick="subscribe_channel('test_channel')">Subscribe</button>
<button onclick="unsubscribe_channel('test_channel')">Unsubscribe</button>
<script src="http://js.pusher./2.2/pusher.min.js"></script>
<script>
Pusher.log = function(msg){
console.log(msg);
};
var YOUR_APP_KEY = "1afb3f8f61eb29da86df";
var pusher = new Pusher(YOUR_APP_KEY);
function subscribe_channel( channelName ) {
pusher.subscribe( channelName );
}
function unsubscribe_channel(channelName) {
pusher.unsubscribe(channelName);
}
</script>
You can find a working example of this code here: http://jsbin./camul/1/edit?html,console,output
本文标签: javascriptUnsubscribe Pusher channelStack Overflow
版权声明:本文标题:javascript - Unsubscribe Pusher channel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745303121a2652491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论