Socket.io node.js,如何避免记录连接时间或说明页面刷新/多个套接字? [英] Socket.io node.js, how to log connection times avoid or account for page refresh/multiple sockets?

查看:132
本文介绍了Socket.io node.js,如何避免记录连接时间或说明页面刷新/多个套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我完全被socket.io,套接字的实际含义等弄糊涂了.我需要做的是将通过"id"(作为URL参数)标识的两个用户连接到房间,也定义为网址参数,例如:

By now, I'm completely confused by socket.io, what a socket actually is, etc. What I need to be able to do is connect two users identified via an 'id' (as a URL parameter) to a room, also defined as a URL parameter like so:

http://www.example.com/room1/user1
http://www.example.com/room1/user2

客户端:

var socket = io();

在服务器上:

app.get('/:id/:role', function(request, response) {
        var room = request.params.id;
        var user = request.params.role;
        io.sockets.on('connection', function(socket){
                      socket.join(room);
                      console.log(user+' has connected to '+room)
                      socket.on('disconnect', function(){
                                socket.leave(room);
                                console.log(user +' has disconnected from '+room);
                                });
                      });

        response.render('pages/room');
        });

在这里我需要做的是记录在特定房间"中这2个用户的连接时间和断开时间.连接和断开连接控制台语句可以工作,但是它们最终会被多次记录,并且在刷新页面后,它们开始会被记录很多次:

What I need to be able to do here is log the connection times and disconnect times for those 2 users, in that specific 'room'. Connection and disconnection console statements work, but they end up getting logged multiple times, and upon a page refresh they start to get logged many, many times:

11:20:23 web.1  | user1 has connected to room1 at 1439220023347 //user1 connects
11:20:41 web.1  | user1 has disconnected from room1 at 1439220041842 //user 1 navigates away from page
11:20:44 web.1  | user1 has connected to room1 at 1439220044447 //user 1 returns back to page via back button
11:21:01 web.1  | user1 has connected to room1 at 1439220061168 //user 2 connects to room creates 2 new connection log events?
11:21:01 web.1  | user2 has connected to room1 at 1439220061168

11:23:04 web.1  | user1 has disconnected from room1 at 1439220184527 //user 1 hits 'refresh' all hell breaks loose
11:23:04 web.1  | user1 has connected to room1 at 1439220184727
11:23:04 web.1  | user2 has connected to room1 at 1439220184727
11:23:04 web.1  | user1 has connected to room1 at 1439220184727

如何到达每个用户仅触发一次断开/连接事件的地步,以便我可以在服务器端可靠地记录该事件? (最终保存到数据库,但是为了便于阅读而省略了.)

How can I get to a point where a disconnect/connect event is fired only once for each user so I can log the event(s) reliably server side? (ultimately saving to a DB, but left out for ease of reading).

推荐答案

io.sockets.on('connection', function(socket){ ...行是套接字的主要事件处理程序,它只需触发一次,否则每次都会重新创建一个处理程序您的路线被称为.通常看起来像这样:

The line io.sockets.on('connection', function(socket){... is the main event handler for your socket, it needs to fire only once or else you'll be recreating a handler everytime your route is called. Typically it would look like this :

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

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

如您所见,路由处理完全独立于套接字事件处理.

As you can see, the route handling is totally independant from the socket event handling.

现在,您要使用URL参数加入会议室.由于套接字连接是由客户端建立的,因此会在路由以Express方式调用之后发生,因此,客户端可以将这些参数发送到套接字. (未经测试的)简单示例.

Now, you want to use URL parameters to join rooms. Because the socket connection is made by the client, it occurs after the route has been called in express, therefore, the client can send those parameters to the socket. (Untested) simplistic example.

客户:

var socket = io.connect('http://localhost:5000'); //adjust to the port your server is listening on
socket.on('auth',function(){
   socket.emit('userParameters',{room:room,user:user});
});

服务器:

app.get('/:id/:role', function(request, response) {
   response.render('pages/room');
});

io.on('connection', function(socket){
    socket.emit('auth'); //will be emitted only once to each socket.
    socket.on('userParameters',function(params){
         socket.join(params.room);
         socket.user = params.user;
         console.log(socket.user+' has connected to '+params.room)
    });
    socket.on('disconnect', function(){
          console.log(socket.user+' has disconnected'); //disconnecting automatically removes the socket from the room.
    });
});

我强烈建议您阅读文档",尤其是Express示例.

I strongly recommend that you read the documentation, especially the Express examples.

此外,请考虑您将在线阅读的许多文档都是关于Socket.IO< 1.0的,并且语法上也有一些区别. 在此处阅读以避免混淆

Also, please consider that a lot of the documentation you'll read online is about Socket.IO<1.0, and there are a few differences in the syntax. Read here to avoid confusion

这篇关于Socket.io node.js,如何避免记录连接时间或说明页面刷新/多个套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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