您的位置:首页技术文章
文章详情页

websocket - node.js express使用socket-io時,要如何做帳號驗證?

浏览:81日期: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储存起来,登录时,emit(’login’,id),服务端响应更新该socket的id,退出则删除,socket.id=null,socketManager.remove(’xx’)。

标签: web