Node.js - Socket.io - Express 3

时间:2022-08-22 15:26:16

I'm a noob to Node.js and Express :( I'm having an issue with accessing the socket.io object in other modules. I have created a global variable to hold a socket object however as soon as that user closes the connection the connection isn't available anymore. Technically I guess the connection still lives on but I delete it for resource reasons.

我是Node.js和Express的菜鸟:(我在访问其他模块中的socket.io对象时遇到问题。我创建了一个全局变量来保存套接字对象,但是一旦用户关闭连接连接不再可用。从技术上讲,我猜连接仍然存在但我出于资源原因删除了它。

Notice below everytime a connection is made we assign that socket to the global variable which is accessible to all other modules.

每次建立连接时都要注意下面我们将该套接字分配给所有其他模块都可以访问的全局变量。

// App.js
var io = require('socket.io').listen(server); 
var sessionsConnections = {};

io.sockets.on('connection', function (socket) 
{
     global.socket = socket;
     sessionsConnections[socket.id] = socket;
     .....
}

socket.on("disconnect", function() 
{
    delete sessionsConnections[socket.id];
});

// Match.js
     global.socket.emit('lobby:createMatch', data);

If the connection last assigned to the global closes Match.js will be screwed. At this point Match.js is the only module who will ever need that socket.io reference. Match.js has a bunch of exports for handling events, emitting the changes and rendering the view.

如果最后分配给全局的连接关闭Match.js将被拧紧。此时,Match.js是唯一需要该socket.io引用的模块。 Match.js有一堆导出来处理事件,发出更改并呈现视图。

Are there any suggestions to how this is handled? Is it possible to instantiate an initial socket connection to live in App.js for the purpose of being a global reference?

对于如何处理这个有什么建议吗?为了成为全局引用,是否可以实例化一个初始套接字连接以存在于App.js中?

2 个解决方案

#1


4  

The socket variable in io.sockets.on('connection', function(socket) {...}) is different for each connection.

io.sockets.on('connection',function(socket){...})中的socket变量对于每个连接都是不同的。

Since in your code global.socket is always a reference to the socket relative to the last connected client, it is normal that if this client disconnects, this socket will die.

因为在你的代码中,global.socket总是引用相对于最后连接的客户端的套接字,所以如果这个客户端断开连接,这个套接字就会死掉是正常的。

In any case, I don't see any reason to use a global variable. If you need to send a message to a specific client, you can use the socket variable inside the connection callback:

无论如何,我认为没有任何理由使用全局变量。如果需要向特定客户端发送消息,可以在连接回调中使用套接字变量:

io.sockets.on('connection', function (socket) 
{
  socket.emit('foo', 'bar');
}

If you need to access the sockets in another module, you can export the sessionsConnections object, and access the socket you need by its id:

如果需要访问另一个模块中的套接字,可以导出sessionsConnections对象,并通过其id访问所需的套接字:

//In app.js
exports.connections = sessionsConnections;

//In match.js
var app = require('./app.js');
app.connections[clientId].emit('foo', 'bar');

Of course, you need to keep track of the id's somewhere.

当然,你需要跟踪id的某个地方。

#2


0  

You might try express.io,

你可以试试express.io,

http://express-io.org/

http://express-io.org/

npm install express.io

It works the same as express, except that it has socket.io integration built-in. It also has basic routing, so that it is a little easier to deal with your issue.

它的工作方式与express相同,只是它内置了socket.io集成。它还具有基本路由,因此处理您的问题要容易一些。

Check out this simple example:

看看这个简单的例子:

app = require('express.io')()
app.http().io()

app.io.route('some-event', function(req) {
    req.io.broadcast('announce', 'someone triggered some-event')
})

app.listen(7076)

You can also do routing with objects, which makes it more like a traditional controller:

您还可以使用对象进行路由,这使它更像传统的控制器:

app = require('express.io')()
app.http().io()

app.io.route('posts', {
    create: function(req) {
        // create a post
    },
    remove: function(req) {
        // remove a post
    }
})

app.listen(7076)

Hope that helps!

希望有所帮助!

#1


4  

The socket variable in io.sockets.on('connection', function(socket) {...}) is different for each connection.

io.sockets.on('connection',function(socket){...})中的socket变量对于每个连接都是不同的。

Since in your code global.socket is always a reference to the socket relative to the last connected client, it is normal that if this client disconnects, this socket will die.

因为在你的代码中,global.socket总是引用相对于最后连接的客户端的套接字,所以如果这个客户端断开连接,这个套接字就会死掉是正常的。

In any case, I don't see any reason to use a global variable. If you need to send a message to a specific client, you can use the socket variable inside the connection callback:

无论如何,我认为没有任何理由使用全局变量。如果需要向特定客户端发送消息,可以在连接回调中使用套接字变量:

io.sockets.on('connection', function (socket) 
{
  socket.emit('foo', 'bar');
}

If you need to access the sockets in another module, you can export the sessionsConnections object, and access the socket you need by its id:

如果需要访问另一个模块中的套接字,可以导出sessionsConnections对象,并通过其id访问所需的套接字:

//In app.js
exports.connections = sessionsConnections;

//In match.js
var app = require('./app.js');
app.connections[clientId].emit('foo', 'bar');

Of course, you need to keep track of the id's somewhere.

当然,你需要跟踪id的某个地方。

#2


0  

You might try express.io,

你可以试试express.io,

http://express-io.org/

http://express-io.org/

npm install express.io

It works the same as express, except that it has socket.io integration built-in. It also has basic routing, so that it is a little easier to deal with your issue.

它的工作方式与express相同,只是它内置了socket.io集成。它还具有基本路由,因此处理您的问题要容易一些。

Check out this simple example:

看看这个简单的例子:

app = require('express.io')()
app.http().io()

app.io.route('some-event', function(req) {
    req.io.broadcast('announce', 'someone triggered some-event')
})

app.listen(7076)

You can also do routing with objects, which makes it more like a traditional controller:

您还可以使用对象进行路由,这使它更像传统的控制器:

app = require('express.io')()
app.http().io()

app.io.route('posts', {
    create: function(req) {
        // create a post
    },
    remove: function(req) {
        // remove a post
    }
})

app.listen(7076)

Hope that helps!

希望有所帮助!