我在让pusher工作时遇到了麻烦。我已经按照文档操作了,但我不知道问题是..控制台返回null。
public function broadcastOn()
{
return new PrivateChannel('my-channel');
}这是我为pusher准备的js。
<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script>
(function () {
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;var pusher =新推流器(‘6049410e84e42d918b14’,{ encrypted: true });
var channel = pusher.subscribe('my-channel');
channel.bind('\Dms\Events\NewNotification', addMessage);
function addMessage(data) {
var listItem = $("<li class='list-group-item'></li>");
listItem.html(data.message);
$('#messages').prepend(listItem);
console.log(data.message)
}
})()
上面是我用来测试的所有代码。请任何人谁做了这方面的协助。Laravel 5.4是目前正在使用的。以下是错误代码。enter image description here
发布于 2017-03-18 19:37:52
在Laravel 5.4中,私有通道添加了前缀private-。因此,请尝试更改以下内容:
var channel = pusher.subscribe('my-channel');要这样做:
var channel = pusher.subscribe('private-my-channel');其次,检查你的字符串,你需要转义反斜杠。所以'\Dms\Events\NewNotification'应该是这样的:'\\Dms\\Events\\NewNotification'
最后,我推荐使用Laravel Echo,因为它使得使用Pusher和Laravel变得非常容易。使用Echo,下面这两行代码:
var channel = pusher.subscribe('my-channel');
channel.bind('\Dms\Events\NewNotification', addMessage);将如下所示:
Echo.private('my-channel')
.listen('\\Dms\\Events\\NewNotification', addMessage);请注意,您不再需要编写前缀private-。
https://stackoverflow.com/questions/42872749
复制相似问题