0

On my website, I have different different pages, each page has javascript that contains

var socket = io.connect('http://localhost:8888');

On the app.js (server file), it's coded so that everytime it receives a connection, it'll do something, no matter which page the client is from.

io.sockets.on ('connection', function (socket) { 
    // do something
});

However, I'd like the app.js to execute different action depending on the page client is from, is that possible?

Thanks!

4

2 回答 2

1

有可能。有两种方法:

1)您可以将标头与来自客户端的数据一起发送。您可以通过以下方式检索它:

String userAgent = request.getHeader("user-agent"); //ter accepting connec Browser identifier.
String remoteAddr = request.getRemoteAddr();

2)或者您可以发送一个额外的变量来存储客户端的ID号并且在服务器端您可以将所有客户端ID存储在一个数组中,并且在接受连接后,您可以使用if-else来确定客户端的ID匹配并相应地执行任务。

于 2013-08-15T00:42:48.633 回答
0

仅当您将当前页面发送到服务器时,或者仅发送 location.href 以使其对每个页面都通用。SocketIO 不能神奇地看到请求来自哪个页面。

于 2013-08-15T00:40:48.547 回答