如何在 Parse.com 中使用推送器专用频道授权



我在互联网上没有找到任何有用的东西,所以我想分享一下。

在客户端上:

var pusher = new Pusher('your-app-key', {authTransport: 'parse'});
Pusher.authorizers.parse = function (socketId, callback) {
    var pusherData = { 
        socket_id: socketId, 
        channel_name: this.channel.name 
    };
    Parse.Cloud.run('authorizePusherChannel', pusherData, {
        success: function (result) {
            callback(false, JSON.parse(result));
        },
        error: function (error) {
            callback(true, error);
        }
    });
};

在解析云上:

Parse.Cloud.define('authorizePusherChannel', function (request, response) {
    if (!request.user) { response.error('User should be autenticated.'); }
    var pusherAppKey = 'your-pusher-app-key';
    var pusherAppSecret = 'your-pusher-app-secret';
    var stringToSign = request.params.socket_id + ':' + request.params.channel_name;
    var authToken = pusherAppKey + ':' + crypto.createHmac('sha256', pusherAppSecret).update(stringToSign).    digest('hex');    
    response.success(JSON.stringify({auth:authToken}));
});
Pusher.authorizers.parse = function (socketId, callback) {
    var pusherData = { 
        socket_id: socketId, 
        channel_name: this.channel.name 
    };
    Parse.Cloud.run('authorizePusherChannel', pusherData, {
        success: function (result) {
            callback(false, JSON.parse(result));
        },
        error: function (error) {
            callback(true, error);

最新更新