ExpressJS,MongoDB + Socket.IO – 识别单个用户会话

我使用https://github.com/sahat/hackathon-starter/tree/es6 。 我添加了Socket.IO,但无法find安全获取当前login用户信息的方法。 我有一个button,当按下时,我想发送到服务器(用户名,余额和金额,所以我可以安全地更新他们的模式模型…到目前为止,我能够如此与玉,但我敢肯定,这是非常糟糕的。

玉:

.content-r-inner.child .col-md-12.col-xs-12.col-lg-12.no-pad-home if !user include partials/flash include partials/visitorbar else include partials/userbar | #time.timecontainer.pulsing.pulsingbox | .product Product: | input#amount(type='text') | #slider-range-max | #minus1.minus.unselectable - | #plus1.plus.unselectable + // </div> .sendAmount(onclick="GetAmount()") // inside include partials/userbar I have GetAmount Function function GetAmount(){ socket.emit('getAmount', { "username": "#{user.username}", "maxBalance": "#{user.balance}", "getAmount": document.getElementById('amount').value }); } 

{user.username}你可以看到里面的标签…

 function GetAmount(){ socket.emit('getAmount', { "username": "JohnDoe", "maxBalance": "3750", "getAmount": document.getElementById('amount').value }); } 

我相信有一个更安全的方式来确定哪个用户按下了button…希望有人可以解释我可以用Socket.IO实现它的最佳方式,刚开始使用它,我发现它会好得多比Ajax在许多方面,但目前缺乏对它的方法,甚至Mongo的基本理解:))

感谢您的帮助 ! 荣誉!!!

看看socketio-jwt ( https://github.com/auth0/socketio-jwt )我认为这就是你所追求的。 基本上你需要authentication活跃的用户。