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

柔情痞子 提交于 2019-12-23 08:54:26

问题


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:


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.



来源:https://stackoverflow.com/questions/26044769/how-can-i-get-the-referer-for-a-socket-io-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!