【问题标题】:pusher authentication socket id undefinedpusher 身份验证套接字 id 未定义
【发布时间】:2013-10-20 22:52:24
【问题描述】:

我正在尝试创建一个私有通道以允许用户将数据发送到我的 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 变量?

【问题讨论】:

    标签: javascript node.js openshift pusher


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!我错过了“app.use(express.bodyParser());”这一行我在所有错误的地方寻找修复。
    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 2019-07-10
    • 2014-04-21
    • 2013-06-02
    • 2019-10-13
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    相关资源
    最近更新 更多