我在服务器上使用此代码发送emit事件并从客户端侦听“ack”。但是,如果客户端断开连接或永远不会收到emit事件,我想继续发送该事件,直到客户端发送“确认”。
io.on('connection', function (socket) {
redis.setConnectionId(socket.decoded_token.connection_prefix + socket.decoded_token.id, socket.id);
socket.on('connectionInterrupt', async function () {
if(socket.decoded_token.connection_prefix == riderPrefix) {
//messages that not yet received by client
const messages = await redis.getAllSocketEventsNotConsumed(riderPrefixMessageQueue + socket.decoded_token.id);
//user socketId
const riderConnectionId = await redis.getConnectionId(riderPrefix + socket.decoded_token.id);
if(messages) {// so some message not received by user
if(messages['driverAccepted'] != null) {//this event not received by client
let travel = await mysql.travel.getById(messages['driverAccepted']);
if(travel.status != mysql.travel.TRAVEL_ERROR_DRIVER_CANCELED) {
const driver = await mysql.driver.getDriverInfo(travel.fk_driver);
console.log('send msg again to socketId:' + riderConnectionId);
//send faild event again and if this time received by client remove from redis
io.sockets.connected[riderConnectionId].emit('driverAccepted', driver, async function() {
console.log('rider ack');
await redis.removeSocketEmitEvent(riderPrefixMessageQueue+travel.fk_rider, 'driverAccepted');
});
}
}
}
}
});
});