首页 > 解决方案 > laravel/beyondcode pusher 不使用 react-native 返回不可用状态

问题描述

const PusherClient = new Pusher('pusherAppKey001', {
    encrypted:true,
    wssPort:6001,
    wsHost:`${PUSHER_SERVER}`,
    cluster:'mt1',
    disableStats:true,
    authEndpoint: `${PUSHER_SERVER}/broadcasting/auth`,
    auth:{
      headers: { 
        Authorization: "Bearer " + jwtToken,
        Accept: "application/json"
    }
    },

});


PusherClient.connection.bind('state_change',(state)=>
{
  console.log(state);
   console.log(state.current);
})

如果我使用 pusher 中的密钥,它可以正常工作,但根据超出代码文档,我们可以在我们的 .env 文件中设置任何 app_id,key... ,它会正常工作

标签: reactjslaravelreact-nativewebsocketpusher

解决方案


经过几个小时尝试不同的事情后终于解决了,事实证明,因为我在我的本地机器上工作,我需要将加密设置为错误,将传输介质设置为 ws,并更改我的默认 URL,我当前的设置如下所示:

useEffect(() => {

    Pusher.logToConsole = true;

    let PusherClient = new Pusher('cf33f3r45fe34',{
        cluster: 'mt1',
        wsHost: PUSHER_SERVER,
        wsPort: '6001',
        encrypted:false,
        enabledTransports: ['ws'],
        forceTLS: false
    });

    let echo = new Echo({
        broadcaster: 'pusher',
        client: PusherClient
    });

    PusherClient.connection.bind('state_change',(data)=>
    {
      console.log('did it work?')
      console.log(data);
    })

    echo.channel('home').listen('NewMessage', (e) => {
        alert()
    });

}) 

请注意,我的推送服务器是我的 IP 地址,没有附加 http,更像是“192.168.33.12”


推荐阅读