我在我的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,请分享您的经验。
1条答案
按热度按时间enyaitl31#
经过几个小时的调试,问题是主体应该作为对象发送
身份验证请求转到以下函数:
如果你尝试Log::info($request),你会发现你的代码中的请求是空数组,所以要解决这个问题,你只需要将主体作为对象即可