简体   繁体   English

Node.js - Socket.io - Express 3

[英]Node.js - Socket.io - Express 3

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. 如果最后分配给全局的连接关闭Match.js将被拧紧。 At this point Match.js is the only module who will ever need that socket.io reference. 此时,Match.js是唯一需要该socket.io引用的模块。 Match.js has a bunch of exports for handling events, emitting the changes and rendering the view. 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中?

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: 如果需要向特定客户端发送消息,可以在连接回调中使用socket变量:

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的某个地方。

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. 它的工作方式与express相同,只是它内置了socket.io集成。 It also has basic routing, so that it is a little easier to deal with your issue. 它还具有基本路由,因此处理您的问题要容易一些。

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! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM