-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ReadJSON and WriteJSON to ws connection (#5)
* feat: add ReadJSON and WriteJSON to ws connection * fix: add CloudWeGo license header Co-authored-by: Nicholas <[email protected]>
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |