From dd8762667d1441876c34ac8bde1077076d74cae5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 25 Oct 2020 17:49:41 +0000 Subject: [PATCH] giving structure to requests... --- client.go | 6 +++--- command.go | 1 + effect.go | 5 +++++ request.go | 8 +++++++- server.go | 6 ++++++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 command.go create mode 100644 effect.go diff --git a/client.go b/client.go index 6d04b3a..38e84d8 100644 --- a/client.go +++ b/client.go @@ -18,11 +18,11 @@ type client struct { port int lastSeq int conn *websocket.Conn - outbox chan request + outbox chan requestBody } func (c *client) run(ctx context.Context) { - c.outbox = make(chan request) + c.outbox = make(chan requestBody) dialer := websocket.Dialer{ HandshakeTimeout: 3 * time.Second, @@ -114,7 +114,7 @@ func (c *client) send(cmd string, args map[string]interface{}) { eargs[k] = json.RawMessage(b) } c.lastSeq++ - req := request{ + req := requestBody{ Seq: c.lastSeq, Command: cmd, Args: eargs, diff --git a/command.go b/command.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/command.go @@ -0,0 +1 @@ +package main diff --git a/effect.go b/effect.go new file mode 100644 index 0000000..50d1088 --- /dev/null +++ b/effect.go @@ -0,0 +1,5 @@ +package main + +type effect interface { + apply(*room) +} diff --git a/request.go b/request.go index b20ec58..268f97b 100644 --- a/request.go +++ b/request.go @@ -2,7 +2,9 @@ package main import "encoding/json" -type request struct { +// requestBody is a container structure that contains the data of a user +// request. This is what clients serialize directly. +type requestBody struct { // client-side sequence number. Clients are expected to send requests with // a monotonically-incrementing sequence number. Seq int `json:"seq"` @@ -15,3 +17,7 @@ type request struct { // executed on the server. Args map[string]json.RawMessage `json:"args"` } + +type request struct { + body requestBody +} diff --git a/server.go b/server.go index 1e51da9..7e99a2f 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "io/ioutil" "net/http" @@ -44,6 +45,11 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { break } s.Info("received: %s", text) + var body requestBody + if err := json.Unmarshal(text, &body); err != nil { + s.Error("unable to parse request: %v", err) + break + } case websocket.BinaryMessage: }