2022-11-22 22:28:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"echo/hero"
|
|
|
|
"echo/net"
|
|
|
|
|
|
|
|
flatbuffers "github.com/google/flatbuffers/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RequestBody() *bytes.Reader {
|
|
|
|
b := flatbuffers.NewBuilder(0)
|
|
|
|
r := net.RequestT{Player: &hero.WarriorT{Name: "Krull", Hp: 100}}
|
|
|
|
b.Finish(r.Pack(b))
|
2022-11-29 02:29:48 +00:00
|
|
|
return bytes.NewReader(b.FinishedBytes())
|
2022-11-22 22:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReadResponse(r *http.Response) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unable to read request body: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:29:48 +00:00
|
|
|
res := net.GetRootAsResponse(body, 0)
|
2022-11-22 22:28:01 +00:00
|
|
|
player := res.Player(nil)
|
|
|
|
|
|
|
|
fmt.Printf("Got response (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
body := RequestBody()
|
|
|
|
req, err := http.NewRequest("POST", "http://localhost:8080/echo", body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := http.DefaultClient
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadResponse(resp)
|
|
|
|
}
|