【问题标题】:401 error when connecting to Pusher from React Native从 React Native 连接到 Pusher 时出现 401 错误
【发布时间】:2018-09-17 01:32:28
【问题描述】:

我正在尝试从 React Native 订阅 Pusher 频道,但是,我收到 401 错误。

我已经读到这可能是服务器时间设置的错误,但已确保这是正确的。使用 Laravel Echo 以及我在 expo 上对其进行测试时,该连接可以完美运行。

我唯一略有不同的是我的时区是欧洲/伦敦,目前比 UTC 早一个小时。当我进入 Pusher 调试控制台时,所有时间都是 UTC。

如果有人有任何想法,这是我的代码:

PusherInit(){
    Pusher.logToConsole = true;
    let chat = 'private-chat.' + this.state.chat_id;
    var pusher = new Pusher('...........', {
      authEndpoint: 'https://snippie.co.uk/api/pusher-auth',
        auth: {
            headers: {
                'Accept': 'application/json',
                'Authorization': this.state.token
            }
        },
      cluster: 'eu',
      encrypted: true
    });

    const channel = pusher.subscribe(chat);

    channel.bind('Message_Received',
        function(data) {
            console.log(data);
        }
    );

    channel.bind('pusher:subscription_error', function(status) {
        console.log (status);
        if(status == 408 || status == 503){
          // retry?
        }
      });
  }

【问题讨论】:

    标签: react-native real-time pusher http-status-code-401 pusher-js


    【解决方案1】:

    尝试将标题更改为:

    headers: {
        'Accept': 'application/json',
        'Authorization': 'Bearer ' + this.state.token
    }
    

    或者

    pusher.config.auth.headers.authorization = 'Bearer ' + this.state.token;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多