使用Pusher时,出现错误后出现无效签名的问题:

Invalid signature: Expected HMAC SHA256 hex digest of socketID:channel, but got hash.


这是我的代码:

public static PusherOptions pusherChannelAuthorise(){
        Log.v(TAG, "pusherChannelAuthorise");
        mMobileToken = Handler_Login.fetchmobile();
        PUSHER_MOBILETOKEN_MAP.put(PUSHER_MOBILETOKEN_LABEL, mMobileToken);

        HttpAuthorizer authoriser = new HttpAuthorizer(url);
        authoriser.setHeaders(PUSHER_MOBILETOKEN_MAP);
        options = new PusherOptions().setAuthorizer(authoriser);

        return options;
    }

    public static void connect(){
        Log.v(TAG, "connect" + "::CONNECTED::");
        PusherOptions presenceChannelOptions = pusherChannelAuthorise();
        presenceChannelConnected = true;
        pusher = new Pusher(pusherHash, presenceChannelOptions);
        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
                socketId = pusher.getConnection().getSocketId();
                Log.v(TAG, "The socketId is: " + socketId);
            }

            @Override
            public void onError(String message, String code, Exception e) {
                Log.v(TAG, "There was a problem connecting!");
            }
        }, ConnectionState.CONNECTED);
        presencechannel = pusher.subscribePresence(PUSHER_PUBLIC_CHANNEL_NAME, listener);
        String myUuid = Handler_Login.getMyUuid();
        privatechannel = pusher.subscribePrivate(DOCSYNC_CHANNEL + myUuid, docSyncListener);
        privatechannel.bind("client-init", docSyncListener = new PrivateChannelEventListener() {
            @Override
            public void onEvent(String channelName, String eventName, String data) {
                Log.v(TAG, "Private Test onEvent: " + channelName + "  " + eventName + "  " + data);
            }

            @Override
            public void onSubscriptionSucceeded(String channelName) {
                Log.v(TAG, "Private Channel onSubscriptionSucceeded: " + channelName);
            }

            @Override
            public void onAuthenticationFailure(String message, Exception e) {
                Log.v(TAG, "Private Channel onAuthenticationFailure: " + message + ":::" + e);
            }
        });
    }


我究竟做错了什么?我是否必须在授权者的标题中设置其他参数? @leggetter

对状态通道的身份验证正常,但专用通道失败。

编辑:
服务器代码(PHP,Laravel):

public function postMobilePusher(Request $request)
    {
        if (null !== $request->header('mobileToken')) {

            $currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();

            if (null !== $currentUser) {

                define('APP_ID', ID);
                define('APP_KEY', KEY);
                define('APP_SECRET', SECRET);

                $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);

                $socket_id = $request->get('socket_id');
                $channel_name = $request->get('channel_name');

                $user_id = $currentUser->id;

                $user_info = [
                    'firstName' => $currentUser->firstName
                    ];

                return $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );

            }

        }

        return abort(403, 'Unauthorized action.');

    }

最佳答案

更新服务器端代码,以将socket_auth用于私有渠道,将presence_auth用于在场渠道:

public function postMobilePusher(Request $request)
{
    if (null !== $request->header('mobileToken')) {

        $currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();

        if (null !== $currentUser) {

            define('APP_ID', ID);
            define('APP_KEY', KEY);
            define('APP_SECRET', SECRET);

            $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);

            $socket_id = $request->get('socket_id');
            $channel_name = $request->get('channel_name');

            $auth = null;
            if(starts_with($channel_name, 'private-')) {
              // TODO: check user has permission to access channel
              $auth = $pusher->socket_auth($channel_name, $socket_id);
            }
            else {
              // presence
              // TODO: check user has permission to access channel
              $user_id = $currentUser->id;

              $user_info = [
                'firstName' => $currentUser->firstName
              ];

              $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
            }



        }

    }

    return abort(403, 'Unauthorized action.');

}

关于android - Android Pusher无效签名-客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33754518/

10-14 13:19