Socket.io客户端请求原始URL [英] Socket.io Client Request Origin URL
本文介绍了Socket.io客户端请求原始URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在客户端的浏览器中,我有以下代码:
On the client, in the browser, I have this code:
this.socket.on('initial', function(data) {
console.log(data);
});
在服务器上,我有:
socket.sockets.on('connection', function(client){
console.log('client connected');
});
我的问题是:如何检测请求来源的URL?例如,如果第一个闭包在"/posts/view/1"上运行,我希望能够在第二个闭包内检测到它.
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.
推荐答案
您可以将此数据发送回服务器.在细节上有些动摇,但是类似:
You could send this data back to the server. A little hand-wavey on the details, but how about something like:
在客户端上:
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);
});
在服务器上:
this.sockets.on('phone_home', function(url) {
console.log("The URL was " + url);
});
这篇关于Socket.io客户端请求原始URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文