giving structure to requests...

master
Jordan Orelli 4 years ago
parent 07791b0752
commit dd8762667d

@ -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,

@ -0,0 +1 @@
package main

@ -0,0 +1,5 @@
package main
type effect interface {
apply(*room)
}

@ -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
}

@ -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:
}

Loading…
Cancel
Save