Node.js - socket.io parse requested url

I want to parse the requested URL. When I used websocket.io, I had a request object that I could parse. How can I do this with socket.io?

example:

http://localhost:4000/foo

how can i get out of "foo"? I tried:

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


    console.log(socket.handshake.address.address);
            console.log(socket.handshake.url);
}

but he does not print "foo"

+5
source share
3 answers

Use the URL api. http://nodejs.org/docs/latest/api/url.html

u = url.parse(url)
console.log(u.path)
+1
source

BTW. Your approach is correct if you supply parameters for socketio connection instead of path elements. This solved my problem when using https://github.com/pkyeck/socket.IO-objc with nodejs socketio to add parameters to my socket connection.

:

[socketIO connectToHost:@"localhost" onPort:8888 withParams:[NSDictionary dictionaryWithObject:@"52523f26f5b23538f000001c" forKey:@"player"]];

URL- :

http://localhost:8888/socket.io/1/?t=16807&player=52523f26f5b23538f000001c

socket.handshake.query

socketio.listen(server).on('connection', function(socket) {
console.log('socket info = ', socket.handshake.query.player);
+3

URL-:

io.on('connection', function(conn){
    console.log(conn.handshake.headers.referer);
});
0

All Articles