如何在 golang 中回复 nats.Subscribe 中已接受的连接

How to reply an accepted connection in nats.Subscribe in golang

我正在尝试在 golang 中创建一个接受连接、读取数据然后通过 nats.Publish 发布的 tcp 服务器。但我也想回复并关闭订阅者内部的连接,而不是在主服务器脚本中 我看到两种可能的方法:发布连接或在订阅者中创建一个新连接,但我都无法实现 提前谢谢你

更新 关于 I. Kozlovic 回答的评论: 不完全是我想要的,但可能会有所帮助:) 我写的是关于关闭 tcp 连接——不是 nats 连接,而是在另一个 go 进程中关闭——而不是在它被接受的过程中。但是由于我没有成功,我可以根据您的回答执行以下操作:我可以通过 nats 发布我需要的信息,对其进行处理,然后用 "OK. Close connection" 回复并关闭作为 TCP 服务器的发布者中的 TCP 连接.但是,如果我不能从这个 go 过程中而是从另一个过程中回复,那就太好了。我明白这很棘手,但我想做我在上一条消息中写的同样的事情,但有一些修改。我有 TCP 服务器,它接受连接,然后连接 nats 并发布消息,订阅此消息的另一个 go 进程处理输入,然后将其输出发布到第三个 go 进程,该进程处理其输入,然后回复 "OK. Done. Close TCP connection" 到第一个去处理.... :)

根据您更新后的问题,这是一种可能的方法。 请注意,这两个额外的进程在这里由 go-routines 表示,但在实际情况下,您会让它们成为单独的进程。 我也省略了错误检查。

// This represent what would be the last process in your
// example.
go func() {
    nc, _ := nats.Connect(nats.DefaultURL)
    nc.Subscribe("bar", func(m *nats.Msg) {
        fmt.Printf("Received request: %s, final stop, sending back to %v\n", m.Data, m.Reply)
        nc.Publish(m.Reply, []byte("I'm here to help!"))
    })
    nc.Flush()
    runtime.Goexit()
}()

// This would be the in-between process that receives
// the message triggered by the TCP accept
go func() {
    nc, _ := nats.Connect(nats.DefaultURL)
    nc.Subscribe("foo", func(m *nats.Msg) {
        fmt.Printf("Received request: %s, forward to bar\n", m.Data)
        nc.PublishRequest("bar", m.Reply, []byte(fmt.Sprintf("got %s", m.Data)))
    })
    nc.Flush()
    runtime.Goexit()
}()

// This would be your TCP server
l, _ := net.Listen("tcp", "127.0.0.1:1234")
for {
    c, _ := l.Accept()
    go func(c net.Conn) {
        // Close socket when done
        defer c.Close()
        // Connect to NATS
        nc, _ := nats.Connect(nats.DefaultURL)
        // Close NATS connection when done
        defer nc.Close()
        // Sends the request to first process. Note that this
        // has a timeout and so if no response is received, the
        // go-routine will exit, closing the TCP connection.
        reply, err := nc.Request("foo", []byte("help"), 10*time.Second)
        if err != nil {
            fmt.Printf("Got error: %v\n", err)
        } else {
            fmt.Printf("Got reply: %s\n", reply.Data)
        }
    }(c)
}

请注意,通常不建议创建非常短暂的 NATS 连接。如果适合您的模型,您可能希望重新使用 NATS 连接。