Flutter Pusher专用通道通知问题

z0qdvdin  于 2023-04-13  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我在我的flutter应用程序中使用pusher-channels-flutter包,后端是Laravel。当我连接到Pusher时,它给了我这个错误。

[  +65 ms] I/PusherChannelsFlutter(20214): Start com.pusher.client.Pusher@4083fc0
[ +102 ms] I/flutter (20214): LOG: Connection: CONNECTING
[+1566 ms] I/flutter (20214): LOG: Connection: CONNECTED
[+1566 ms] I/flutter (20214): LOG: onSubscriptionError: Unable to parse response from Authorizer: null Exception: com.pusher.client.AuthorizationFailureException: Unable to parse
response from Authorizer: null

这是推送器初始化函数。

void onConnect() async {
    if (!_channelFormKey.currentState!.validate()) {
      return;
    }

    try {
      await pusher.init(
        apiKey: _apiKey,
        cluster: _cluster,
        onConnectionStateChange: onConnectionStateChange,
        onError: onError,
        onSubscriptionSucceeded: onSubscriptionSucceeded,
        onEvent: onEvent,
        onSubscriptionError: onSubscriptionError,
        onDecryptionFailure: onDecryptionFailure,
        onMemberAdded: onMemberAdded,
        onMemberRemoved: onMemberRemoved,
        //authEndpoint: "https://my-website.com/broadcasting/auth",
        onAuthorizer: onAuthorizer,
      );
      await pusher.subscribe(channelName: _channelName.text);
      await pusher.connect();
    } catch (e) {
      print("$e");
      log("ERROR: $e");
    }
  }

这是onAuthorizer函数

dynamic onAuthorizer(
      String channelName, String socketId, dynamic options) async {
    var authUrl = "https://my-website.com/broadcasting/auth";
    var result = await http.post(
      Uri.parse(authUrl),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ${token}',
      },
      body: 'socket_id=' + socketId + '&channel_name=' + channelName,
    );
    return jsonDecode(result.body);
  }

似乎请求authUrl = "https://my-website.com/broadcasting/auth"返回null,我尝试了Content-Type作为json,但返回相同的结果。

headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ${token}',
      }

如果有人集成了推杆和Flutter,请分享您的经验。

enyaitl3

enyaitl31#

经过几个小时的调试,问题是主体应该作为对象发送
身份验证请求转到以下函数:

// Illuminate\Broadcasting\Broadcasters\PusherBroadcaster.php

/**
 * Authenticate the incoming request for a given channel.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return mixed
 *
 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
 */
public function auth($request)
{
    $channelName = $this->normalizeChannelName($request->channel_name);

    if (
        empty($request->channel_name) ||
        ($this->isGuardedChannel($request->channel_name) &&
            !$this->retrieveUser($request, $channelName))
    ) {
        throw new AccessDeniedHttpException;
    }

    return parent::verifyUserCanAccessChannel(
        $request,
        $channelName
    );
}

如果你尝试Log::info($request),你会发现你的代码中的请求是空数组,所以要解决这个问题,你只需要将主体作为对象即可

dynamic onAuthorizer(String channelName, String socketId, dynamic options) async {
    var authUrl = authEndpoint;
    try {
      var result = await http.post(
        Uri.parse(authUrl),
        headers: {
          'Accept': 'application/json',
          'Authorization': 'Bearer ${userToken}',
        },
        body: {'socket_id': socketId, 'channel_name': channelName},
      );
      var jsonResponse = jsonDecode(result.body);
      print("Authorization response: $jsonResponse");
      return jsonResponse;
    } catch (e) {
      print("Error during authorization: $e");
      throw e;
    }
  }

相关问题