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:
到现在为止,我完全被socket.io,socket实际上是什么等等所困惑。我需要做的是将通过'id'(作为URL参数)识别的两个用户连接到一个房间,定义为URL参数,如下所示:
http://www.example.com/room1/user1
http://www.example.com/room1/user2
Client side:
var socket = io();
On the server:
在服务器上:
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');
});
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).
我怎样才能达到每个用户只触发一次disconnect / connect事件的点,这样我就可以在服务器端可靠地记录事件了? (最终保存到数据库,但为了便于阅读而遗漏)。
1 个解决方案
#1
2
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 :
行io.sockets.on('connection',function(socket){...是套接字的主要事件处理程序,它只需要触发一次,否则每次调用路径时都会重新创建一个处理程序。通常它看起来像这样:
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.
如您所见,路由处理完全独立于套接字事件处理。
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.
现在,您希望使用URL参数来加入会议室。因为套接字连接是由客户端进行的,所以它是在express中调用路由后发生的,因此,客户端可以将这些参数发送到套接字。 (未经测试)简单化的例子。
Client :
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});
});
Server :
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.
});
});
I strongly recommend that you read the documentation, especially the Express examples.
我强烈建议您阅读文档,尤其是Express示例。
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 <1.0的,并且语法上有一些差异。请阅读此处以避免混淆
#1
2
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 :
行io.sockets.on('connection',function(socket){...是套接字的主要事件处理程序,它只需要触发一次,否则每次调用路径时都会重新创建一个处理程序。通常它看起来像这样:
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.
如您所见,路由处理完全独立于套接字事件处理。
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.
现在,您希望使用URL参数来加入会议室。因为套接字连接是由客户端进行的,所以它是在express中调用路由后发生的,因此,客户端可以将这些参数发送到套接字。 (未经测试)简单化的例子。
Client :
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});
});
Server :
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.
});
});
I strongly recommend that you read the documentation, especially the Express examples.
我强烈建议您阅读文档,尤其是Express示例。
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 <1.0的,并且语法上有一些差异。请阅读此处以避免混淆