在JS+PHP (宅基地)环境中,订阅Pusher的公共频道很好(也可以验证我的证书)。订阅专用信道失败,给定以下代码:
let theAppId = 'XXXYYYZZZ'; //fake credentials shown here...
pusher = new Pusher(theAppId, {
authEndpoint: '/pusher/auth',
cluster: 'us2',
forceTLS: true,
encrypted: true,
auth: {headers: {'X-CSRF-Token': self.csrf}}
});
channel = pusher.subscribe('private-channel1');调用我的auth代码并返回有效的auth sig:
{\"auth\":\"c289b20c368bd23a4a85:d55f1f1495f0d252b5fde1d69e2e6d5b4b161ca49cab5ad218d65111ae307a12\"}"}成功连接后,Pusher.log将显示此错误:
Pusher : Event recd : {"event":"pusher:error","data":{"code":null,"message":"Invalid key in subscription auth data: '{\"auth\"'"}}
pusher.min.js:8 Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Invalid key in subscription auth data: '{\"auth\"'"}}}我在推车医生里找不到关于这个问题的任何提及。有人看到这个,或者对如何解决有想法吗?
发布于 2019-02-04 19:11:10
造成此错误的最可能原因是,在从服务器返回auth哈希之前,它是“字符串化”的。这就是为什么错误消息提到找到了一个无效的键:不能将该值解析为JSON对象。
要解决这个问题,服务器需要返回普通的JSON。
发布于 2019-02-04 19:56:08
谢谢Hosam的暗示。对于您的Laravel用户来说,问题很容易解决。请注意,来自您的auth路由的响应是json。从pusher->socket_auth创建有效的auth键所返回的值是一个json编码的字符串。要解决这个问题,只需将json_decode响应传递给关联数组,然后在控制器json响应中传递该响应:
$thePusher = new \Pusher\Pusher(
env('PUSHER_APP_KEY'),env('PUSHER_APP_SECRET'),env('PUSHER_APP_ID'),
['cluster'=>env('PUSHER_CLUSTER'),'useTLS'=>true]
);
$theResult=$thePusher->socket_auth(
$aRequest>input('channel_name'),
$aRequest->input('socket_id'));
return response()->json(json_decode($theResult,true),200);https://stackoverflow.com/questions/54500953
复制相似问题