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
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
github
nhooyr
websocket
Commits
6eda9c56
Unverified
Commit
6eda9c56
authored
Jun 23, 2019
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Docs for CloseRead
parent
559c1697
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
example_test.go
+1
-5
1 addition, 5 deletions
example_test.go
websocket.go
+8
-5
8 additions, 5 deletions
websocket.go
with
9 additions
and
10 deletions
example_test.go
+
1
−
5
View file @
6eda9c56
...
...
@@ -74,11 +74,7 @@ func Example_writeOnly() {
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
(),
time
.
Minute
*
10
)
defer
cancel
()
go
func
()
{
defer
cancel
()
c
.
Reader
(
ctx
)
c
.
Close
(
websocket
.
StatusPolicyViolation
,
"server doesn't accept data messages"
)
}()
ctx
=
c
.
CloseRead
(
ctx
)
t
:=
time
.
NewTicker
(
time
.
Second
*
30
)
defer
t
.
Stop
()
...
...
This diff is collapsed.
Click to expand it.
websocket.go
+
8
−
5
View file @
6eda9c56
...
...
@@ -22,7 +22,7 @@ import (
// and SetReadLimit.
//
// You must always read from the connection. Otherwise control
// frames will not be handled. See the docs on Reader.
// frames will not be handled. See the docs on Reader
and CloseRead
.
//
// Please be sure to call Close on the connection when you
// are finished with it to release the associated resources.
...
...
@@ -319,10 +319,8 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
// to be closed so you do not need to write your own error message.
// This applies to the Read methods in the wsjson/wspb subpackages as well.
//
// You must read from the connection for close frames to be read.
// If you do not expect any data messages from the peer, just call
// Reader in a separate goroutine and close the connection with StatusPolicyViolation
// when it returns. See the writeOnly example.
// You must read from the connection for control frames to be handled.
// If you do not expect any data messages from the peer, call CloseRead.
//
// Only one Reader may be open at a time.
//
...
...
@@ -388,6 +386,11 @@ func (c *Conn) reader(ctx context.Context) (MessageType, io.Reader, error) {
return
MessageType
(
h
.
opcode
),
r
,
nil
}
// CloseRead will close the connection if any data message is received from the peer.
// Call this when you are done reading data messages from the connection but will still write
// to it. Since CloseRead is still reading from the connection, it will respond to ping, pong
// and close frames automatically. It will only close the connection on a data frame. The returned
// context will be cancelled when the connection is closed.
func
(
c
*
Conn
)
CloseRead
(
ctx
context
.
Context
)
context
.
Context
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
go
func
()
{
...
...
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