如何在Parse.com上使用Pusher私人频道授权

时间:2014-03-02 23:48:16

标签: javascript parse-platform pusher

我没有在互联网上找到任何有用的内容,所以我想分享。

2 个答案:

答案 0 :(得分:6)

在客户端:

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}));
});

答案 1 :(得分:0)

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);