Socket.io Client Request Origin URL

心不动则不痛 提交于 2019-12-24 11:29:29

问题


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

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