node.js – socket.ioparsing请求的url
我想分析一个请求的url。 当我使用websocket.io时,我有一个请求对象,我可以parsing。 我怎么能用socket.io做到这一点?
例:
http://localhost:4000/foo
我怎样才能摆脱“富”? 我试过了:
io.sockets.on('connection', function (socket) { console.log(socket.handshake.address.address); console.log(socket.handshake.url); }
但它不打印“foo”
使用URL API。 http://nodejs.org/docs/latest/api/url.html
u = url.parse(url) console.log(u.path)
顺便说一句 – 你的方法是正确的,如果你是提供参数到socketio连接,而不是path元素。 它解决了我使用https://github.com/pkyeck/socket.IO-objc与nodejs socketio添加参数到我的套接字连接的问题。
例如:
[socketIO connectToHost:@"localhost" onPort:8888 withParams:[NSDictionary dictionaryWithObject:@"52523f26f5b23538f000001c" forKey:@"player"]];
构buildURL如下:
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);
我正在访问这样的url:
io.on('connection', function(conn){ console.log(conn.handshake.headers.referer); });