如何使用rxjs连接到WebSocket私有通道[已关闭]

2wnc66cl  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(170)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
你好,我想使用rxjs连接到一个私有的WebSocket通道,有人能给我指出正确的方向吗?文档或者教程
我能够成功连接到WebSocket,但现在我想订阅专用频道

rqmkfv5c

rqmkfv5c1#

我最终使用了推送器而不是Rxjs

this.pusher = new Pusher(environment.pusherKey, {
  wsHost: environment.pusherHost,
  wsPort: port,
  wssPort: port,
  forceTLS: true,
  disableStats: true,
  enabledTransports: ['ws', 'wss'],
  authEndpoint: `${apiUrl}/auth/pusher`,
  auth: {
    headers: {
      Authorization: `Bearer ${
        this.authService.getAuthFromLocalStorage().authToken
      }`,
    },
  },
});

当然,我必须在后端服务中使用islaravel通过以下方法进行身份验证

public function pusherAuth(Request $request){
    $user = auth()->user();
    $socket_id = $request['socket_id'];
    $channel_name =$request['channel_name'];
    $key = config('broadcasting.connections.pusher.key');
    $secret = config('broadcasting.connections.pusher.secret');
    $app_id = config('broadcasting.connections.pusher.app_id');

    if ($user) {
        $authData = json_encode([
            'user_id' => $user->id,
            'user_info' => [
                'name' => $user->name
            ]
        ]);
        $pusher = new Pusher($key, $secret, $app_id);
        $auth = $pusher->socket_Auth($channel_name, $socket_id,$authData);

        return response($auth, 200);

    }
    return new Response('Unauthorized', 401);
}

注意,我使用了一个php调试器,它改变了给我这个错误的默认auth响应
Error: JSON returned from channel-authorization endpoint was invalid, yet status code was 200.
要解决这个问题,我们要么从.env关闭调试,要么从调试栏配置“except选项”关闭调试

相关问题