- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在广播/授权路由上收到 403。
我正在为网络套接字使用 Laravel websockets 包,我有以下设置
- Backend Laravel server ( on port 8000 )
- Laravel Websockets Server ( laravel is running on 8001 and websocket server on 6001 )
- Standalone Ionic React app ( on port 8100)
现在,当我尝试公共(public) channel 时一切正常,但当我尝试私有(private) channel 时却失败了。
在上面的 Laravel 套接字仪表板屏幕截图中,我可以看到已建立连接,因为这是与请求一起使用的相同 socketId。
我正在使用 Laravel Sanctum 进行身份验证。
PFB :- 我的客户端代码
const headers = {
'Content-Type': 'application/json',
Authorization: window.localStorage.getItem('token')
? `Bearer ${window.localStorage.getItem('token')}`
: '',
'Access-Control-Allow-Credentials': true,
};
const options = {
broadcaster: 'pusher',
key: 'abcsock',
authEndpoint: 'http://localhost:8001/api/broadcasting/auth',
authorizer: (channel, options) => {
return {
authorize: (socketId, callback) => {
axios
.post(
'http://localhost:8001/api/broadcasting/auth',
{
socket_id: socketId,
channel_name: channel.name,
},
{ headers, withCredentials: true }
)
.then((response) => {
callback(false, response.data);
})
.catch((error) => {
callback(true, error);
});
},
};
},
wsHost: '127.0.0.1',
wsPort: 6001,
encrypted: true,
disableStats: true,
enabledTransports: ['ws', 'wss'],
forceTLS: false,
};
来自 Laravel Websockets 服务器的代码
api.php文件
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Broadcast;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Broadcast::routes(['middleware' => ['auth:sanctum']]);
BroadcastServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;
class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Broadcast::routes(['middleware' => ['auth:sanctum']]);
require base_path('routes/channels.php');
}
}
NewNotification.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Broadcasting\PrivateChannel;
use App\AppNotification;
use App\User;
class NewNotification extends Notification
{
use Queueable;
public $notification;
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(AppNotification $notification, User $user)
{
$this->notification = $notification;
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
$notification = $this->notification;
return [
'id' => $notification->id,
'title' => $notification->title,
'description' => $notification->description,
'button_text' => $notification->button_text,
'is_read' => $notification->is_read,
'created_at' => $notification->created_at,
[
'notification_for' => $notification->notifiable
]
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage($this->toArray($notifiable));
}
public function broadcastOn()
{
return new PrivateChannel('Notifications.' . $this->user->id);
}
}
channel .php
<?php
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('Notifications.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
}, ['guards' => ['sanctum']]);
我已附上我认为足以解释问题并供您进行故障排除的所有内容,但如果您需要更多信息,请询问。
任何帮助将不胜感激
最佳答案
我喜欢这个包,因为它是用 php 编写的,但它有点被遗弃,还有很多问题没有解决。
此服务器的替代方案是 laravel-echo-server(如 laravel 文档所推荐的),使用起来非常简单。 https://stackoverflow.com/a/61704796/7908390更多信息,如果你可以使用 laravel-sockets 我很高兴看到一个新的例子
关于laravel - PrivateChannel 403 错误 Laravel Echo Pusher Laravel Websockets 本地设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62058958/
我不确定我是否正确理解 authentication在 Pusher 工作。这是我担心的一个场景: 用户想要订阅私有(private) channel ,因此 Pusher 库调用我的服务器以获得身份
典型的聊天应用。使用存在 channel 来判断谁在线,但正在寻找一种优雅的方式来使用空闲标志在存在 channel 中标记用户。 最佳答案 对此的完整解决方案可能相当复杂,而且还取决于运行时 - 我
Pusher 似乎是一项不错的服务,但我想知道是否有人知道它是否可以用作持久事件流。例如,我想订阅一个 channel 并获取历史事件,而不仅仅是订阅后的新实时事件事件。 最佳答案 channel 事
我找不到任何关于使用 Pusher 创建 channel 的明确信息。在客户端订阅或在服务器上推送事件时,这只是一个隐式操作吗? class HelloController 'world' })
我有 40 个类别,每个类别都有 10-100 个子类别。默认情况下,用户收听所有类别和子类别。我想让每个用户能够选择从整个类别或特定子类别中取消绑定(bind)。所以,现在我所拥有的是每个类别都是一
是否可以从 API 获取与推送器 (http://pusher.com/) 的事件连接数? 在他们支持的 Python 服务器库中找不到它。虽然我可以在他们基于 Web 的仪表板的统计页面上看到它,但
我想要什么:在 Pusher 中订阅私有(private) channel 后如何获得订阅计数。实际上我只想在 2 个用户之间进行私有(private)聊天。 我所知道的和正在做的事情: .我知道我的
我最近在我的 PHP laravel 项目中使用了 pusher,它运行良好。 我对 pusher 的了解是,它是我们的服务器和客户端之间的实时层,并创建到我们的客户端浏览器的 Web 套接字连接。
我不确定我是否正确理解authentication在Pusher中的工作方式。这是我担心的情况: 用户想要订阅专用频道,因此Pusher库调用我的服务器以获得身份验证令牌。 服务器检查用户是否已登录并
我正在编写多人国际象棋游戏,并在 websocket 服务器部分使用 Pusher。 无论如何,如果我有一个用户列表,并且我选择其中的任何一个并挑战他们,我如何仅向该用户发送挑战?我知道我会使用客户端
我正在使用 Backbone 前端,我们目前正在使用推送器使其实时化。大多数事件都是从我们的 API 触发的,然后我们在主干前端的 channel 上监听它们。 但是,在某些情况下,我们希望通过单击按
我在我的 php 文件中集成了一些脚本。它正在读取 Pusher channel ,并在列出的 channel 上有新事件时执行多项操作。 如果我在浏览器上运行: http:/localhost/pu
我正在使用 nextjs 在 Web 应用程序中实现推送器。它在我的开发环境中按预期工作。但当我将其部署到 vercel 时,它无法正常工作。我只有在浏览器中刷新页面时才能看到结果。 这是客户端的实现
我正在使用 PHP 和 Javscript (jQuery) 测试 Pusher 库,以通过一个相当简单的应用程序来处理 Websocket,您可以在这里找到它:http://jsfiddle.net
我正在开发的实时应用程序遇到问题。我的问题是关于使用 Pusher 的用户状态。例如,我想获得正确的方法来检测用户何时离开页面或保持一段时间不活动以将其状态设置为“离线”。我怎样才能做到这一点?我几乎
我正在尝试实现一项功能,通知用户推送器断开连接,并指示何时重新连接。我的第一个实验只是将更改的推送器状态记录到控制台: var pusher = new Pusher('MY_ACCOUNT_STRI
我正在开发的实时应用程序遇到问题。我的问题是关于使用 Pusher 的用户状态。例如,我想获得正确的方法来检测用户何时离开页面或保持一段时间不活动以将其状态设置为“离线”。我怎样才能做到这一点?我几乎
我正在使用适用于 iOS 的 Pusher 客户端,通过 CocoaPods 安装它(pod 'libPusher', '~> 1.5')。 一切都安排得很好,事件进展顺利。但是,如果设备(运行 iO
我正在使用 Pusher 将事件推送到我订阅的浏览器。 当我向我的服务器发送更新时,我的服务器发出一个 Push 事件,我的客户端(发起请求)和任何其他订阅的客户端都会被触发。 我遇到的问题是事件发起
我正在使用 Pusher 提供的起始代码,当我将其放入基本的 swift 项目并向其发送消息时,没有任何反应。我也安装了 cocoa pod 包。 应打印语句“已收到数据”。 也没有错误。 最佳答案
我是一名优秀的程序员,十分优秀!