gpt4 book ai didi

javascript - Pusher 身份验证套接字 ID 未定义

转载 作者:行者123 更新时间:2023-12-02 18:13:49 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于javascript - Pusher 身份验证套接字 ID 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com