Skip to content

Commit

Permalink
feat: add ReadJSON and WriteJSON to ws connection (#5)
Browse files Browse the repository at this point in the history
* feat: add ReadJSON and WriteJSON to ws connection

* fix: add CloudWeGo license header

Co-authored-by: Nicholas <[email protected]>
  • Loading branch information
niczy and Nicholas committed Sep 24, 2022
1 parent 0453d14 commit 98f8051
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
48 changes: 48 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2017 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// This file may have been modified by CloudWeGo authors. All CloudWeGo
// Modifications are Copyright 2022 CloudWeGo Authors.

package websocket

import (
"encoding/json"
"io"
)

// WriteJSON writes the JSON encoding of v as a message.
//
// See the documentation for encoding/json Marshal for details about the
// conversion of Go values to JSON.
func (c *Conn) WriteJSON(v interface{}) error {
w, err := c.NextWriter(TextMessage)
if err != nil {
return err
}
err1 := json.NewEncoder(w).Encode(v)
err2 := w.Close()
if err1 != nil {
return err1
}
return err2
}

// ReadJSON reads the next JSON-encoded message from the connection and stores
// it in the value pointed to by v.
//
// See the documentation for the encoding/json Unmarshal function for details
// about the conversion of JSON to a Go value.
func (c *Conn) ReadJSON(v interface{}) error {
_, r, err := c.NextReader()
if err != nil {
return err
}
err = json.NewDecoder(r).Decode(v)
if err == io.EOF {
// One value is expected in the message.
err = io.ErrUnexpectedEOF
}
return err
}
94 changes: 94 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2017 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// This file may have been modified by CloudWeGo authors. All CloudWeGo
// Modifications are Copyright 2022 CloudWeGo Authors.

package websocket

import (
"bytes"
"encoding/json"
"io"
"reflect"
"testing"
)

func TestJSON(t *testing.T) {
var buf bytes.Buffer
wc := newTestConn(nil, &buf, true)
rc := newTestConn(&buf, nil, false)

var actual, expect struct {
A int
B string
}
expect.A = 1
expect.B = "hello"

if err := wc.WriteJSON(&expect); err != nil {
t.Fatal("write", err)
}

if err := rc.ReadJSON(&actual); err != nil {
t.Fatal("read", err)
}

if !reflect.DeepEqual(&actual, &expect) {
t.Fatal("equal", actual, expect)
}
}

func TestPartialJSONRead(t *testing.T) {
var buf0, buf1 bytes.Buffer
wc := newTestConn(nil, &buf0, true)
rc := newTestConn(&buf0, &buf1, false)

var v struct {
A int
B string
}
v.A = 1
v.B = "hello"

messageCount := 0

// Partial JSON values.

data, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
for i := len(data) - 1; i >= 0; i-- {
if err := wc.WriteMessage(TextMessage, data[:i]); err != nil {
t.Fatal(err)
}
messageCount++
}

// Whitespace.

if err := wc.WriteMessage(TextMessage, []byte(" ")); err != nil {
t.Fatal(err)
}
messageCount++

// Close.

if err := wc.WriteMessage(CloseMessage, FormatCloseMessage(CloseNormalClosure, "")); err != nil {
t.Fatal(err)
}

for i := 0; i < messageCount; i++ {
err := rc.ReadJSON(&v)
if err != io.ErrUnexpectedEOF {
t.Error("read", i, err)
}
}

err = rc.ReadJSON(&v)
if _, ok := err.(*CloseError); !ok {
t.Error("final", err)
}
}

0 comments on commit 98f8051

Please sign in to comment.