Socket.io - How to get client URL request on serve

2019-03-30 07:00发布

In my app.js (server) I need to know from where (URL) the request came.

Currently, I'm passing the URL as parameter from the client:

socket.emit('request', window.location.href);

and processing it server side

socket.on('request', function(url){
    console.log(url);
    ...
});

But that's clearly risky and unsecure (clients can send anything to the server).

So I'm guessing.. is it possible to get the URL parameter only on server side? Maybe from the socket object?

1条回答
老娘就宠你
2楼-- · 2019-03-30 07:08

To obtain the connection URL

io.sockets.on('connection', function(socket) {

    console.log("url: " + socket.handshake.url);

});

This will return something like: url: /socket.io/1/?t=1407807394827

查看更多
登录 后发表回答