How can I get the referer for a socket.io connecti

2019-06-19 02:32发布

I have a simple node.js web server and I use socket.io.

The users can perform simple tasks on the server, which is a single page application running on /. If a user has an existing state on the server, the user's ID could be appended to the URL, e.g. /{my-id}.

I need to extract the ID from the URL when a socket.io connection is established. Is it any way to get the URL for the client page where the socket is created from; the referrer of the connection request?

1条回答
看我几分像从前
2楼-- · 2019-06-19 02:55

Using console.log I managed to find this value like this:

io.on('connection', function (socket) {
    var referer = socket.request.headers.referer;
});

It works, but as it is not documented, I don't know if this is recommended, or if it's going to change.

查看更多
登录 后发表回答