2

我正在尝试使用 Go 发送 JSON 消息。这是服务器代码:

func (network *Network) Join(
    w http.ResponseWriter,
    r *http.Request) {
    //the request is not interesting
    //the response will be a message with just the clientId value set
    log.Println("client wants to join")
    message := Message{-1, -1, -1, ClientId(len(network.Clients)), -1, -1}
    var buffer bytes.Buffer
    enc := json.NewEncoder(&buffer)

    err := enc.Encode(message)
    if err != nil {
        fmt.Println("error encoding the response to a join request")
        log.Fatal(err)
    }

    fmt.Printf("the json: %s\n", buffer.Bytes())
    fmt.Fprint(w, buffer.Bytes())
}

网络是一个自定义结构。在主函数中,我正在创建一个网络对象并将其方法注册为对 http.HandleFunc(...) 的回调

func main() {
    runtime.GOMAXPROCS(2)
    var network = new(Network)
    var clients = make([]Client, 0, 10)
    network.Clients = clients

    log.Println("starting the server")
    http.HandleFunc("/request", network.Request)
    http.HandleFunc("/update", network.GetNews)
    http.HandleFunc("/join", network.Join)
    log.Fatal(http.ListenAndServe("localhost:5000", nil))
}

Message 也是一个结构。它有六个字段,都是 int 的类型别名。当客户端向 url "localhost:5000/join" 发送 http GET 请求时,应该会发生这种情况

  • 调用网络对象上的 Join 方法
  • 为客户端创建了一个带有 Id 的新消息对象
  • 此消息被编码为 JSON
  • 要检查编码是否正确,将编码的消息打印在 cmd
  • 消息被写入 ResponseWriter

客户端比较简单。它具有与 Message 结构完全相同的代码。在主函数中,它只是向“localhost:5000/join”发送一个 GET 请求并尝试解码响应。这是代码

func main() {

    // try to join
    var clientId ClientId
    start := time.Now()
    var message Message
    resp, err := http.Get("http://localhost:5000/join")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(resp.Status)
    dec := json.NewDecoder(resp.Body)
    err = dec.Decode(&message)
    if err != nil {
        fmt.Println("error decoding the response to the join request")
        log.Fatal(err)
    }

    fmt.Println(message)
    duration := time.Since(start)
    fmt.Println("connected after: ", duration)
    fmt.Println("with clientId", message.ClientId)
}

我已经启动了服务器,等待了几秒钟,然后运行了客户端。这是结果

  • 服务器打印“客户想要加入”
  • 服务器打印 "json: {"What":-1,"Tag":-1,"Id":-1,"ClientId":0,"X":-1,"Y":-1}"
  • 客户端打印“200 OK”
  • 客户端崩溃“错误解码加入请求的响应”
  • 错误是“数组元素后的无效字符“3””

这个错误信息真的让我很困惑。毕竟,在我的 json 中,没有数字 3。所以我在客户端上导入了 io/ioutil 并使用此代码打印了响应

b, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("the json: %s\n", b)

请注意,打印语句与服务器上的相同。我希望看到我编码的 JSON。相反,我得到了这个

  • “200 好”
  • “json:[123 34 87 104 97 116 ....]”这个列表持续了很长时间

我是新手,不知道我这样做是否正确。但似乎上面的代码只是打印了字节片。奇怪,在服务器上输出被转换为字符串。

我的猜测是,我以某种方式读取了错误的数据,或者消息在服务器和客户端之间的途中被损坏了。但老实说,这些只是疯狂的猜测。

4

2 回答 2

5

在您的服务器中,而不是

fmt.Fprint(w, buffer.Bytes())

你需要使用:

w.Write(buffer.Bytes())

fmt 包会将 Bytes() 格式化为人类可读的切片,其中字节表示为整数,如下所示:

[123 34 87 104 97 116 ... etc
于 2013-05-19T15:08:07.227 回答
3

您不想使用fmt.Print将内容写入响应。例如

package main

import (
    "fmt"
    "os"
)

func main() {
    bs := []byte("Hello, playground")
    fmt.Fprint(os.Stdout, bs)
}

游乐场链接

生产

[72 101 108 108 111 44 32 112 108 97 121 103 114 111 117 110 100]

改用 ResponseWriter 的Write () 方法

您可以通过远程登录到您的服务器作为实验来发现这一点 - 当您不确定发生了什么时,这总是一个好主意!

于 2013-05-19T15:08:32.750 回答