React Native Pusher Private Channel onAuthorization不工作

tpxzln5u  于 2023-10-22  发布在  React
关注(0)|答案(1)|浏览(113)


React Native pusher onAuthrization不工作。它的工作为公共频道,但当我尝试为私人然后onAuthorization不工作

useEffect(() => {
        getConnectToPusher();
    }, []);

    const getConnectToPusher = async () => {
        console.log('Calling')
        const pusher = Pusher.getInstance();
        const channels = {};
        await pusher.init({
            apiKey: '',
            cluster: 'ap1',
            onAuthorizer: (channelName, socketId) => {
                console.warn(channelName, socketId)
            },
            onError(message, code, e) {
                console.log(`onError: $message code: ${code} exception: ${e} message= ${message}`);
            }
        });
        await pusher.connect();

        const myChannel = await pusher.subscribe({
            channelName: 'private-my-channel',
            onSubscriptionSucceeded: (channelName, data) => {
                console.log(`Subscribed to ${channelName}`);
                console.log(`I can now access me: ${myChannel.me}`)
                console.log(`And here are the channel members: ${myChannel.members}`)
            },
            onEvent: (event) => {
                console.log(`Event received: ${event}`);
            },
        });

    }
u5rb5r59

u5rb5r591#

我是这样使用授权器的

authorizer: (channel, options) => {

  return fetch('YOUR_AUTH_ENDPOINT', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      // Add any required headers
    },
    body: JSON.stringify({
      channel_name: channel.name,
      socket_id: options.socket_id,
    }),
  })
  .then(response => response.json())
  .then(data => data.auth);
},

相关问题