文章詳情頁
websocket - node.js express使用socket-io時(shí),要如何做帳號(hào)驗(yàn)證?
瀏覽:120日期:2023-10-04 10:55:46
問題描述
問題解答
回答1:Socket#request:RequestA getter proxy that returns the reference to the request thatoriginated the underlying engine.io Client. Useful for accessingrequest headers such as Cookie or User-Agent.
socket.io
通過 socket.request.headers.cookie 來得到cookie通過 socket.request.session 來得到session
io.on(’connection’, function(socket){ if(socket.request.session.isLogin()){//....已登入成功 做其他事情 }else{socket.close(); }});回答2:
我是另外把socket按id儲(chǔ)存起來,登錄時(shí),emit(’login’,id),服務(wù)端響應(yīng)更新該socket的id,退出則刪除,socket.id=null,socketManager.remove(’xx’)。
標(biāo)簽:
web
排行榜

熱門標(biāo)簽