问题
On the client, in the browser, I have this code:
this.socket.on('initial', function(data) {
console.log(data);
});
On the server I have:
socket.sockets.on('connection', function(client){
console.log('client connected');
});
My question is: how can I detect the URL where the request came from? For example if the first closure was running on "/posts/view/1", I want to be able to detect it inside of the second closure.
回答1:
You could send this data back to the server. A little hand-wavey on the details, but how about something like:
On the client:
this.socket.on('initial', function(data) {
// do whatever with data
var my_location = get_page_location_with_javascript(); // or whatever
this.socket.emit('phone_home', my_location);
});
On the server:
this.sockets.on('phone_home', function(url) {
console.log("The URL was " + url);
});
来源:https://stackoverflow.com/questions/7422157/socket-io-client-request-origin-url