推送器身份验证套接字ID未定义

时间:2013-10-20 22:52:25

标签: javascript node.js openshift pusher

我正在尝试创建一个私有通道,以允许用户将数据发送到我的node.js服务器。请求失败,并返回推送器:subscription_error,错误代码为500.

我的node.js(服务器端)日志无法将socket_id从传入请求拉到'/ pusher / auth'

app.post( '/pusher/auth', function( req, res ) {
  var socketId = req.body.socket_id;//<--DEBUG: TypeError: Cannot read property 'socket_id' of undefined

  var channel = req.body.channel_name;
  var presenceData = {
    user_id: 'unique_user_id',
    user_info: {
      name: 'Mr Pusher',
      twitter_id: '@pusher'
    }
  };
  var auth = pusher.auth( socketId, channel, presenceData );
  res.send( auth );
} );

以下是发送请求的客户端:

   // Create new Pusher instance and connect
        var key = "<%= appKey %>"  
        var id = "<%= appKey %>"  
        var secret = "<%= appSecret %>"  
        var pusher = new Pusher( "<%= appKey %>" );

// Subscribe to the channel that the event will be published on
var channel = pusher.subscribe( 'private-channel' );
    channel.bind('pusher:subscription_succeeded',function(){
            alert("subscription succeeded")
            var triggered = channel.trigger("client-event",{})
            alert("triggered "+triggered)
    })
    channel.bind('pusher:subscription_error',function(status){
            alert("subscription error "+status)//<-- This error gets returned
    })
// Bind to the event on the channel and handle the event when triggered
channel.bind( 'client-event', function( data ) {
  // For now, alert the message.
  alert( data.message );
} );

这应该由推送器API自动处理,还是我需要明确发送它? 如何将socket_id发送到服务器? 我怎样才能访问客户端上的socket_id变量?

1 个答案:

答案 0 :(得分:2)

听起来您需要设置bodyParser来解析请求正文并使request.body可用。

请参阅Pusher文档:http://pusher.com/docs/authenticating_users#implementing_private_endpoints/lang=node