search

Home  >  Q&A  >  body text

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

巴扎黑巴扎黑2784 days ago635

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 16:01:31

    Socket#request:Request
    A getter proxy that returns the reference to the request that
    originated the underlying engine.io Client. Useful for accessing
    request 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();
        }
    });
    

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 16:01:31

    I store the socket by ID separately. When logging in, emit('login',id), the server responds by updating the socket's id, and deletes it when exiting, socket.id=null, socketManager.remove('xx') .

    reply
    0
  • Cancelreply