我有一个用reactjs,nodejs和expressjs,mongodb和socket io创建的聊天应用程序。一切运行良好,但是当我遇到短暂的互联网连接问题时,后端引发了此错误:
{ MongoError: connection 4 to (database_url:port) timed out
at Function.MongoError.create (/repo_directory/node_modules/mongodb-core/lib/error.js:29:11)
at TLSSocket.<anonymous> (/repo_directory/node_modules/mongodb-core/lib/connection/connection.js:186:20)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7)
at TLSSocket.Socket._onTimeout (net.js:422:8)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
name: 'MongoError',
message: 'connection 4 to (database_url:port) timed out' }
Error: Mubsub: broken cursor.
at Timeout._onTimeout (/repo_directory/node_modules/mubsub/lib/channel.js:159:40)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
然后,即使互联网连接恢复,套接字io也将停止工作。需要重新启动nodejs才能使一切再次正常运行。 现在,是否有一种方法可以仅重新启动套接字io以使其再次正常工作?因此,我不必重新启动整个服务器即可解决此问题。
下面是我的代码:
var server = http.createServer(app);
var io = app.io;
io.attach(server, {
handlePreflightRequest: function (req, res) {
let origin = req.headers['origin']
console.log('handling socket cors..', origin)
var headers = {
'Access-Control-Allow-Headers': 'Content-Type, Authorization, channel, conversationid, conversationsecret, customerid, merchantid',
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Credentials': true
};
res.writeHead(200, headers);
res.end();
}
});
。
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server", moment().format('LLL'));
});
// circular reference for io to be easily passed
var app = express();
var io = socket_io();
app.io = io;
io.on('connection', function(socket) {
// the primary socket at '/'
console.log('user ' + socket.id + ' has connected on /');
});
var setupIO = require('./chat/sockets/socketioManager')(io);
app.set('socketio', setupIO);
socketioManager文件(编辑了一些信息):
module.exports = function(io) {
var ioManager = io;
let mubsubClient = mubsub(config.mongoUrl)
var adapter = mongoAdapter(config.mongoUrl, {
key: 'key',
client: mubsubClient
});
adapter.channel = mubsubClient.channels['key']
ioManager.adapter(adapter);
mubsubClient.on('error', console.error);
adapter.channel.on('error', console.error);
ioManager.on('error', function (err) {
console.log(err.stack);
});
ioManager.on('connection', function(socket) {
console.log( 'connection attempt', socket.customerId, socket.conversationId )
var subscription;
var request = socket.request
var adminSubscription = null;
if ( socket.type === 'spectator' ) {
subscription = SpectatorSubscriber( socket.merchantId, socket, adapter )
} else {
console.log( 'subscribing to', socket.customerId, socket.merchantId )
subscription = CustomerSubscriber( socket.customerId, socket.merchantId, socket, adapter )
}
// pre load conversations
if ( socket.customerId ) {
getMessageList( socket.customerId, 0, 20, ( err, messages ) => {
if(err) console.log(err);
else socket.emit('msglist', messages);
} )
}
console.log('[socket] connection from', socket.channel)
socket.on( 'sendInvite', inviteHandler( ioManager ) )
socket.on( 'sendMsg', sendMessageHandler( ioManager, socket ) )
socket.on( 'sendBlock', sendBlock(ioManager))
socket.on( 'getMsgsWithSkip', function( req, callback ) {
var skipnum = req.skipnum;
getMessageList( socket.customerId, skipnum, 20, ( err , messages ) => {
console.log( 'GETTING PREV MESSAGES', messages.length )
if ( err ) console.log( err )
else callback( messages )
} )
})
socket.on('disconnect', function() {
if ( subscription != null ) subscription.unsubscribe()
if ( adminSubscription != null ) adminSubscription.unsubscribe()
});
});
return ioManager;
}