每页表示特定的socket.io连接 [英] Express specific socket.io connections per page

查看:84
本文介绍了每页表示特定的socket.io连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以目前我的设置是我有一个标准的app.get('/'等用于索引,在这里,我有io.on('connection',function等)。现在的目标是使每个当有人仅连接到主页时,我可以使用io.on(connection来获取该套接字,然后将其发送给它,我的语法和所有内容都很好,但是我相信将io.on('connection'放在路由内部是我的问题:

问题:启动服务器后,只要有人连接到我的网站,它的工作原理就很好,对于调试示例,我在其中包含console.log及其调用一次,我们感觉很好。但是,如果那个人重新加载页面,我的io.on('connection'会再次被调用,这次我会得到2 console.log's ...当我再次重新加载时,我会得到3,依此类推等等,无论我是关闭页面并重新打开页面还是来自其他ip,似乎重新加载时连接都没有关闭,重新加载时所有仍挂起的连接都被撤回了。



我知道我不发布我的代码有点不合常规。它不是语法,这里实际上是描述的示例。哦,我也需要访问来自app.get的req输入,这就是为什么首先在其中存储护照变量的原因。

  app.get('/',函数(req,res){
res.sendFile(__ dirname +' /index.html');

io.on('connection',function(socket){
console.log( 1 connection);
});

});

我希望这足以说明我的问题。当我第一次寻找答案时,我发现了很多有关路由的内容,但感到困惑。

解决方案

对于我从问题和评论中得到的东西,我们深表感谢。
您做错了路。每次插入时,都不要将套接字连接的ON和EMIT多次调用。



例如:第一次调用

  app.get('/',函数(req,res){
res.sendFile(__ dirname +'/ index。 html');
io.on('connection',function(socket){
console.log( 1 connection);
});
});

io.ons ['connection'] 的值为

  function(socket){
console.log( 1连接);
}

第二次调用该回调是附加再次。并且 io.ons ['connection'] 的值现在是

  function(socket){
console.log( 1 connection);
}
function(socket){
console.log( 1 connection);
}

因此它两次打印console.log



SECOND:
,如果您想在用户登录后做进一步的工作。您需要在此处验证用户,可以在客户端
和服务器端使用 socket.emit('init',.... code> socket.on('init',...... 将验证用户并可以访问其他用户,并关闭连接。


So currently my setup is I have a standard app.get('/', etc for my index and inside here, I have my io.on('connection', function etc). Now the goal is so that every time someone connects to only the homepage i can get that socket with io.on(connection and send things to it that way, and my syntax and all is fine however i believe having my io.on('connection' inside a route is my issue.

The problem: Whenever someone connects to my website after i start the server, it works great, for debug examples i have a console.log inside of it and its called once and we are good. However if that same person reloads the page my io.on('connection' is called again, and this time iw ill get 2 console.log's... when I again reload I then get 3 and so on and so on, no matter if i close the page and reopen it or come from a different ip. It seems as if the connection isnt closed when I reload and all the still hanging connections are recalled when I reload.

I know this is a little unorthodox with me not posting my code. Its not the syntax, here is an example of essentially the set up described. Oh and also i need access to the req input from the app.get which is why its in there in the first place, I have passport variables saved in it.

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');

    io.on('connection', function(socket){
        console.log("1 connection");
   });

});

I hope this explains my issue well enough. When i looked for answers first i found a bunch of stuff about routing, but was confused. Any help is appreciated!

解决方案

For what I got from the question and the comments. You are doing it wrong way. You should never put the ONs and EMITs of socket connection where they are called multiple times as they are appended every time.

for example : first time this is called

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
    io.on('connection', function(socket){
        console.log("1 connection");
   });
});

the io.ons['connection'] has the value

function(socket){
   console.log("1 connection");
}

second time you call it the callback is appended again. and the value of io.ons['connection'] is now

function(socket){
    console.log("1 connection");
}
 function(socket){
    console.log("1 connection");
}

So it prints console.log two times

SECOND : if you want to do the further work after the user is logged in.then you need to verify the user here, you can use a socket.emit('init',..... from client side and server-side socket.on('init',...... will verify the user and can access else return and close the connection.

这篇关于每页表示特定的socket.io连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆