当我使用私人频道时,我无法让echo和pusher工作,我在谷歌上搜索了两天,发现了nada。
似乎正在发生的是身份验证的某种问题(我使用的是Laravel ),因为我可以订阅公共频道。
routes/channels.php
Broadcast::channel('private-ci-received-{userId}', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});
Broadcast::channel('private-ci-received-{toUserId}', function ($currentUser, $toUserId) {
return true;
});bootstrap.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
logToConsole: true,
encrypted: true,
});default.blade.php(主布局)
Pusher.logToConsole = true;
Echo.logToConsole = true;
Echo.private('ci-received-{{ Auth::user()->id}}')
.listen('.CIReceived', (e) => {
console.log(e);
});控制台上打印的内容是:
Pusher : No callbacks on private-ci-received-1 for pusher:subscription_error这是一个相当普通的错误,然后为了调试的目的,我尝试用Pusher (不是laravel )绑定错误。
var pusher = new Pusher('MYSECRETAPPKEYHERE', {
cluster: 'us2',
forceTLS: true
});
var channel = pusher.subscribe('private-ci-received-1');
channel.bind('pusher:subscription_error', function(data) {
console.log(data);
});console.log(数据)输出
JSON returned from webapp was invalid, yet status code was 200默认的authEndPoint是/authEndPoint/auth,我认为它希望返回一个JSON,但是它会从我的页面返回JSON代码。
这些路线是由框架本身创建的,根据我所读到的,Laravel和Laravel应该合作得很好,不需要太多的摆弄。
我的.env文件是正确的,我使用pusher作为广播驱动程序,BroadcastServiceProvider没有正确的注释。
有人能说明这件事吗?谢谢
发布于 2020-08-27 14:07:16
,,这对我有用
.env的默认值是
BROADCAST_DRIVER=log
请把它换成
BROADCAST_DRIVER=pusher我还得到了“针对pusher:subscription_error的私有用户1没有回调”
在做了上述修改后,对我来说很好。
发布于 2019-01-17 22:44:30
在您的APP_DEBUG=true文件中设置.env,并在浏览器开发工具的网络部分检查您的auth路由的HTTP。如果身份验证成功,您的laravel应用程序应该使用JSON响应如下:
{"auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}如果出现错误,它将显示在响应中,因为您将调试模式设置为true。
发布于 2020-01-07 16:51:52
您不需要在broadcasting.php中将私有信息添加到您的通道中,这是自动完成的。
尝尝这个
Broadcast::channel('ci-received-{userId}', function ($user, $userId) {
return (int) $user->id === (int) $userId;
});https://stackoverflow.com/questions/54111832
复制相似问题