首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pusher专用信道订阅中的意外错误

Pusher专用信道订阅中的意外错误
EN

Stack Overflow用户
提问于 2019-02-03 07:55:26
回答 2查看 1.1K关注 0票数 0

在JS+PHP (宅基地)环境中,订阅Pusher的公共频道很好(也可以验证我的证书)。订阅专用信道失败,给定以下代码:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
{\"auth\":\"c289b20c368bd23a4a85:d55f1f1495f0d252b5fde1d69e2e6d5b4b161ca49cab5ad218d65111ae307a12\"}"}

成功连接后,Pusher.log将显示此错误:

代码语言:javascript
复制
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\"'"}}}

我在推车医生里找不到关于这个问题的任何提及。有人看到这个,或者对如何解决有想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-04 19:11:10

造成此错误的最可能原因是,在从服务器返回auth哈希之前,它是“字符串化”的。这就是为什么错误消息提到找到了一个无效的键:不能将该值解析为JSON对象。

要解决这个问题,服务器需要返回普通的JSON。

票数 0
EN

Stack Overflow用户

发布于 2019-02-04 19:56:08

谢谢Hosam的暗示。对于您的Laravel用户来说,问题很容易解决。请注意,来自您的auth路由的响应是json。从pusher->socket_auth创建有效的auth键所返回的值是一个json编码的字符串。要解决这个问题,只需将json_decode响应传递给关联数组,然后在控制器json响应中传递该响应:

代码语言:javascript
复制
$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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54500953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档