io-syncify
v1.0.0
Published
跨进程、跨服务器之间通信同步化
Downloads
18
Readme
const {
sleep,
IOSyncify,
Message,
RedisInputMessageReceiver,
RedisMessageSender,
RedisOutputMessageReceiver,
} = require('io-syncify');
const EventEmitter = require('events');
let redisOptions = {
host: 'host',
port: 6379,
password: 'password',
family: 4,
db: 0
};
/**
* 输入消息发布通道
*/
let input_message_pub_channel = 'input_message_pub_channel_test';
/**
* 输出消息订阅通道
*/
let output_message_sub_channel = 'output_message_sub_channel_test';
let ioSyncify = new IOSyncify(
new RedisOutputMessageReceiver(output_message_sub_channel, redisOptions, new EventEmitter()),
new RedisMessageSender(input_message_pub_channel, redisOptions)
);
/**
* 其它服务器或进程,输入消息订阅通道
*/
let input_message_sub_channel = input_message_pub_channel;
/**
* 其它服务器或进程,输出消息发布通道
*/
let output_message_pub_channel = output_message_sub_channel;
let outputMessageSender = new RedisMessageSender(output_message_pub_channel, redisOptions);
let inputMessageReceiver = new RedisInputMessageReceiver(input_message_sub_channel, redisOptions, {
handle(message) {
message.text = Math.random().toString();
message.timestamp = Date.now();
outputMessageSender.send(message);
}
});
inputMessageReceiver.receive();
(async () => {
let i = 0;
await sleep(5000);
while (true) {
await sleep(10);
i++;
setImmediate(async ()=>{
let now = Date.now();
let result = await ioSyncify.input(new Message(`${Date.now()}_${i}`, now.toString()), 10000);
console.log(Date.now(), result.toJsonArrayString(), result.timestamp-now);
});
}
})();