0

我正在尝试创建一个私有通道以允许用户将数据发送到我的 node.js 服务器。请求失败,并返回 pusher: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 变量?

4

1 回答 1

2

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

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

于 2013-10-21T08:59:25.077 回答