這是我關于反應本機代碼的代碼:
const pusher = new Pusher('app_key', {
cluster: 'ap1',
authEndpoint: 'http://10.0.27.124:8070/pusher/auth'
});
const channel = pusher.subscribe('my-channel');
channel.bind('uuganaa', function (data) {
alert(JSON.stringify(data));
});
在 Node.js 代碼中:
app.post('/pusher/auth', (req, res) => {
let socketId = req.body.socket_id;
let channel = req.body.channel_name;
random_string = Math.random().toString(36).replace(/[^a-z] /g, '').substr(0, 5);
let presenceData = {
user_id: random_string,
user_info: {
username: '@' random_string,
}
};
let auth = pusher.authenticate(socketId, channel, presenceData);
res.send(auth);
});
這是我的日志:
Pusher : : ["State changed","initialized -> connecting"]
Pusher : : ["Connecting",{"transport":"ws","url":"wss://ws-ap1.pusher.com:443/app/73286f08a5b2aeeea398?protocol=7&client=js&version=7.0.3&flash=false"}]
Pusher : : ["State changed","connecting -> connected with new socket ID 12784.212561977"]
Pusher : : ["Event sent",{"event":"pusher:subscribe","data":{"auth":"","channel":"my-channel"}}]
這里稱為事件日志:
Pusher : : ["Event recd",{"event":"uuganaa","channel":"my-channel","data":{"message":"Sain uu asd"}}]
并將請求發送到http://10.0.27.124:8070/pusher/auth來自 auth 端點的回應成功回應。
為什么它不觸發 authEndpoint 謝謝你的回答。
uj5u.com熱心網友回復:
那是因為您正在為用戶訂閱公共頻道。公共頻道不需要授權連接。您需要為用戶訂閱私人頻道。
https://pusher.com/docs/channels/using_channels/authorized-connections/
https://pusher.com/docs/channels/using_channels/private-channels/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403181.html
標籤:
上一篇:Latex數學運算式對齊
下一篇:Api認證方法
