尝试学习 nsq,并遵循golang 示例和nsqjs中的示例。我正在服务器端发送消息,使用 for 循环和执行例程
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(x int) {
defer wg.Done()
chanName := fmt.Sprintf("import_progress_587e6442ff74889098498f6e")
m := map[string]interface{}{
"body": map[string]interface{}{
"progress": x,
},
}
msg, _ := json.Marshal(m)
req := NSQPubReq{
Topic: chanName,
Body: msg,
}
if err := producer.Publish(req.Topic, req.Body); err != nil {
}
utils.Info(fmt.Sprintf("sent msg=%v", string(msg)))
}(i)
}
wg.Wait()
但问题是,在客户端。
// channel = 'import_progress_587e6442ff74889098498f6e'
let reader = new nsq.Reader(channel, channel, {
//lookupdHTTPAddresses: '<<IP>>:4161',
maxInFlight: 10000,
snappy: true
})
reader.connect()
reader.on('message', (msg) => {
var msgData = {
id: msg.id,
body: msg.body.toString(),
chan: channel
}
io.emit(channel, msgData)
msg.finish()
})
消息不会立即发送给客户端。我将等待几秒钟,直到消息到达 nodejs 客户端。有什么我需要做的设置吗?谢谢你!