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
3f26c9f6
Unverified
Commit
3f26c9f6
authored
1 year ago
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
wsjson: Write messages in a single frame always
Closes #315
parent
a633a10f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
internal/util/util.go
+7
-0
7 additions, 0 deletions
internal/util/util.go
wsjson/wsjson.go
+9
-8
9 additions, 8 deletions
wsjson/wsjson.go
with
16 additions
and
8 deletions
internal/util/util.go
0 → 100644
+
7
−
0
View file @
3f26c9f6
package
util
type
WriterFunc
func
(
p
[]
byte
)
(
int
,
error
)
func
(
f
WriterFunc
)
Write
(
p
[]
byte
)
(
int
,
error
)
{
return
f
(
p
)
}
This diff is collapsed.
Click to expand it.
wsjson/wsjson.go
+
9
−
8
View file @
3f26c9f6
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"nhooyr.io/websocket"
"nhooyr.io/websocket"
"nhooyr.io/websocket/internal/bpool"
"nhooyr.io/websocket/internal/bpool"
"nhooyr.io/websocket/internal/util"
"nhooyr.io/websocket/internal/errd"
"nhooyr.io/websocket/internal/errd"
)
)
...
@@ -51,17 +52,17 @@ func Write(ctx context.Context, c *websocket.Conn, v interface{}) error {
...
@@ -51,17 +52,17 @@ func Write(ctx context.Context, c *websocket.Conn, v interface{}) error {
func
write
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
,
v
interface
{})
(
err
error
)
{
func
write
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
,
v
interface
{})
(
err
error
)
{
defer
errd
.
Wrap
(
&
err
,
"failed to write JSON message"
)
defer
errd
.
Wrap
(
&
err
,
"failed to write JSON message"
)
w
,
err
:=
c
.
Writer
(
ctx
,
websocket
.
MessageText
)
if
err
!=
nil
{
return
err
}
// json.Marshal cannot reuse buffers between calls as it has to return
// json.Marshal cannot reuse buffers between calls as it has to return
// a copy of the byte slice but Encoder does as it directly writes to w.
// a copy of the byte slice but Encoder does as it directly writes to w.
err
=
json
.
NewEncoder
(
w
)
.
Encode
(
v
)
err
=
json
.
NewEncoder
(
util
.
WriterFunc
(
func
(
p
[]
byte
)
(
int
,
error
)
{
err
:=
c
.
Write
(
ctx
,
websocket
.
MessageText
,
p
)
if
err
!=
nil
{
return
0
,
err
}
return
len
(
p
),
nil
}))
.
Encode
(
v
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to marshal JSON: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to marshal JSON: %w"
,
err
)
}
}
return
nil
return
w
.
Close
()
}
}
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