How do I get the url in io.socket.on("connection") in nodejs?

时间:2018-02-05 12:52:36

标签: node.js

I tried the followings but it does not return the url as string.

 io.on('connection', function (socket) {
  console.log(socket.handshake.url)
})

it prints something like "/socket.io/?EIO=3&transport=polling&t=M5blR4u". How do I get a url like "http://192.168.2.1:3600/app"?

Do I really have to send the url from the user side with window.location?

1 个答案:

答案 0 :(得分:0)

您需要使用socket.handshake.headers.host

io.on('connection', function (socket) {
 console.log(socket.handshake.headers.host)  
});

,输出为:

localhost:8560

或者您可以使用handshake.referer来完成这项工作。

更多详情:Socket.io Documents