2018-09-11 08:25
浏览 92


I am trying to make a tcp server in golang which accepts the connection, reads data and then publishes it via nats.Publish. But I also want to reply and close the connection inside the subsriber not in the main server script I see two possible ways: to publish connection or to make a new one in a subsriber, but I cannot realize either Thank you in advance

UPD A comment regarding I. Kozlovic answer: Not exactly what I wanted but may help :) I was writing about closing tcp connection - not a nats one and to close in another go process - not in the one it was accepted. But as I did not succeeded in it I could do the following basing on your answer: I can publish information I need via nats, process it and then reply with "OK. Close connection" and close the TCP connection in publisher which is a TCP server. However that would be great if I could reply not from this go process but from the another one. Its tricky I understand but I want to do the same I wrote in the previous message but with some modification. I have TCP server, it accepts connection and then connects nats and publishes message, another go process subscribed to this message processes input and then publishes its output to the third go process which processes its input and then replies "OK. Done. Close TCP connection" to the first go process.... :)

图片转代码服务由CSDN问答提供 功能建议

我正尝试在golang中制作一个TCP服务器,该服务器接受连接,读取数据,然后通过nat发布。 发布。 但是我也想在主服务器脚本中而不是在子分配器内部答复并关闭连接 我看到两种可能的方法:发布连接或在子分配器中建立新连接,但我都无法实现, 提前谢谢您< / p>

UPD 关于I. Kozlovic的评论: 不是我想要的,但可能会有所帮助:) 我正在写有关关闭tcp连接的信息-不是一个简单的话题,而是另一个话题 过程-不被接受。 但是由于我没有成功,我可以根据您的回答执行以下操作:我可以通过nat发布所需的信息,对其进行处理,然后单击“确定。关闭连接”,然后在发布者(即TCP)中关闭TCP连接 服务器。 但是,如果我不能从执行过程中而是从另一个过程中进行答复,那将是很棒的。 我理解它很棘手,但是我想要做的与上一条消息中写的一样,但是要进行一些修改。 我有TCP服务器,它接受连接,然后连接nat并发布消息,订阅该消息的另一个go进程处理该输入,然后将其输出发布到第三个go进程,该进程处理其输入,然后回复“确定。完成。关闭TCP连接 ”进行首次尝试。...:)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongy44342
    dongy44342 2018-09-28 20:13

    Based on your updated question, here is a possible approach. Note that the two extra processes are represented by go-routines here, but you would have them be separate processes in real case. I have also omitted error checking.

    // 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
    ", m.Data, m.Reply)
            nc.Publish(m.Reply, []byte("I'm here to help!"))
    // 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
    ", m.Data)
            nc.PublishRequest("bar", m.Reply, []byte(fmt.Sprintf("got %s", m.Data)))
    // This would be your TCP server
    l, _ := net.Listen("tcp", "")
    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
    ", err)
            } else {
                fmt.Printf("Got reply: %s
    ", reply.Data)

    Note that is is usually not recommended to create very short lived NATS connections. You may want to reuse the NATS connection if that fits with your model.

    点赞 评论