good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
websocket
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
websocket
Commits
0e788123
Unverified
Commit
0e788123
authored
5 years ago
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Improve JSON API
Closes #50
parent
e16f8309
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
README.md
+11
-3
11 additions, 3 deletions
README.md
datatype_string.go
+2
-2
2 additions, 2 deletions
datatype_string.go
example_test.go
+10
-2
10 additions, 2 deletions
example_test.go
json.go
+32
-11
32 additions, 11 deletions
json.go
websocket_test.go
+10
-2
10 additions, 2 deletions
websocket_test.go
with
65 additions
and
20 deletions
README.md
+
11
−
3
View file @
0e788123
...
...
@@ -45,13 +45,17 @@ fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
(),
time
.
Second
*
10
)
defer
cancel
()
v
:=
map
[
string
]
interface
{}{
"my_field"
:
"foo"
,
}
err
=
websocket
.
Write
JSON
(
ctx
,
c
,
v
)
err
=
jc
.
Write
(
ctx
,
v
)
if
err
!=
nil
{
log
.
Printf
(
"failed to write json: %v"
,
err
)
return
...
...
@@ -73,7 +77,7 @@ For a production quality example that shows off the low level API, see the [echo
```
go
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
10
)
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Minute
)
defer
cancel
()
c
,
_
,
err
:=
websocket
.
Dial
(
ctx
,
"ws://localhost:8080"
,
...
...
@@ -84,8 +88,12 @@ if err != nil {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
var
v
interface
{}
err
=
websocket
.
Read
JSON
(
ctx
,
c
,
v
)
err
=
jc
.
Read
(
ctx
,
v
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to read json: %v"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
datatype_string.go
+
2
−
2
View file @
0e788123
...
...
@@ -12,9 +12,9 @@ func _() {
_
=
x
[
DataBinary
-
2
]
}
const
_DataType_name
=
"
Text
Binary"
const
_DataType_name
=
"
DataTextData
Binary"
var
_DataType_index
=
[
...
]
uint8
{
0
,
4
,
1
0
}
var
_DataType_index
=
[
...
]
uint8
{
0
,
8
,
1
8
}
func
(
i
DataType
)
String
()
string
{
i
-=
1
...
...
This diff is collapsed.
Click to expand it.
example_test.go
+
10
−
2
View file @
0e788123
...
...
@@ -88,13 +88,17 @@ func ExampleAccept() {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
(),
time
.
Second
*
10
)
defer
cancel
()
v
:=
map
[
string
]
interface
{}{
"my_field"
:
"foo"
,
}
err
=
websocket
.
Write
JSON
(
ctx
,
c
,
v
)
err
=
jc
.
Write
(
ctx
,
v
)
if
err
!=
nil
{
log
.
Printf
(
"failed to write json: %v"
,
err
)
return
...
...
@@ -123,8 +127,12 @@ func ExampleDial() {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
var
v
interface
{}
err
=
websocket
.
Read
JSON
(
ctx
,
c
,
v
)
err
=
jc
.
Read
(
ctx
,
v
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to read json: %v"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
json.go
+
32
−
11
View file @
0e788123
...
...
@@ -7,15 +7,28 @@ import (
"golang.org/x/xerrors"
)
// ReadJSON reads a json message from c into v.
func
ReadJSON
(
ctx
context
.
Context
,
c
*
Conn
,
v
interface
{})
error
{
typ
,
r
,
err
:=
c
.
ReadMessage
(
ctx
)
// JSONConn wraps around a Conn with JSON helpers.
type
JSONConn
struct
{
Conn
*
Conn
}
// Read reads a json message into v.
func
(
jc
JSONConn
)
Read
(
ctx
context
.
Context
,
v
interface
{})
error
{
err
:=
jc
.
read
(
ctx
,
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to read json: %w"
,
err
)
}
return
nil
}
func
(
jc
*
JSONConn
)
read
(
ctx
context
.
Context
,
v
interface
{})
error
{
typ
,
r
,
err
:=
jc
.
Conn
.
ReadMessage
(
ctx
)
if
err
!=
nil
{
return
err
}
if
typ
!=
Text
{
return
xerrors
.
Errorf
(
"unexpected frame type for json (expected Text
Frame
): %v"
,
typ
)
if
typ
!=
Data
Text
{
return
xerrors
.
Errorf
(
"unexpected frame type for json (expected
Data
Text): %v"
,
typ
)
}
r
.
Limit
(
131072
)
...
...
@@ -24,25 +37,33 @@ func ReadJSON(ctx context.Context, c *Conn, v interface{}) error {
d
:=
json
.
NewDecoder
(
r
)
err
=
d
.
Decode
(
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to
read
json: %w"
,
err
)
return
xerrors
.
Errorf
(
"failed to
decode
json: %w"
,
err
)
}
return
nil
}
// WriteJSON writes the json message v into c.
func
WriteJSON
(
ctx
context
.
Context
,
c
*
Conn
,
v
interface
{})
error
{
w
:=
c
.
MessageWriter
(
Text
)
// Write writes the json message v.
func
(
jc
JSONConn
)
Write
(
ctx
context
.
Context
,
v
interface
{})
error
{
err
:=
jc
.
write
(
ctx
,
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to write json: %w"
,
err
)
}
return
nil
}
func
(
jc
JSONConn
)
write
(
ctx
context
.
Context
,
v
interface
{})
error
{
w
:=
jc
.
Conn
.
MessageWriter
(
DataText
)
w
.
SetContext
(
ctx
)
e
:=
json
.
NewEncoder
(
w
)
err
:=
e
.
Encode
(
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to
writ
e json: %w"
,
err
)
return
xerrors
.
Errorf
(
"failed to
encod
e json: %w"
,
err
)
}
err
=
w
.
Close
()
if
err
!=
nil
{
return
x
err
ors
.
Errorf
(
"failed to write json: %w"
,
err
)
return
err
}
return
nil
}
This diff is collapsed.
Click to expand it.
websocket_test.go
+
10
−
2
View file @
0e788123
...
...
@@ -173,10 +173,14 @@ func TestHandshake(t *testing.T) {
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
(),
time
.
Second
*
5
)
defer
cancel
()
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
v
:=
map
[
string
]
interface
{}{
"anmol"
:
"wowow"
,
}
err
=
websocket
.
Write
JSON
(
ctx
,
c
,
v
)
err
=
jc
.
Write
(
ctx
,
v
)
if
err
!=
nil
{
return
err
}
...
...
@@ -191,8 +195,12 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
jc
:=
websocket
.
JSONConn
{
Conn
:
c
,
}
var
v
interface
{}
err
=
websocket
.
Read
JSON
(
ctx
,
c
,
&
v
)
err
=
jc
.
Read
(
ctx
,
&
v
)
if
err
!=
nil
{
return
err
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment