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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
websocket
Commits
4357cbf9
Unverified
Commit
4357cbf9
authored
5 years ago
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Add WriteOnly example
parent
ee1f3c60
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
example_test.go
+42
-0
42 additions, 0 deletions
example_test.go
websocket.go
+4
-1
4 additions, 1 deletion
websocket.go
with
46 additions
and
1 deletion
example_test.go
+
42
−
0
View file @
4357cbf9
...
@@ -59,3 +59,45 @@ func ExampleDial() {
...
@@ -59,3 +59,45 @@ func ExampleDial() {
c
.
Close
(
websocket
.
StatusNormalClosure
,
""
)
c
.
Close
(
websocket
.
StatusNormalClosure
,
""
)
}
}
func
ExampleWriteOnly
()
{
fn
:=
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
AcceptOptions
{})
if
err
!=
nil
{
log
.
Println
(
err
)
return
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
"the sky is falling"
)
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
(),
time
.
Minute
*
10
)
defer
cancel
()
go
func
()
{
defer
cancel
()
_
,
_
,
err
:=
c
.
Reader
(
ctx
)
if
err
==
nil
{
c
.
Close
(
websocket
.
StatusPolicyViolation
,
"server doesn't accept data messages"
)
}
}()
t
:=
time
.
NewTicker
(
time
.
Second
*
30
)
defer
t
.
Stop
()
for
{
select
{
case
<-
ctx
.
Done
()
:
c
.
Close
(
websocket
.
StatusNormalClosure
,
""
)
return
case
<-
t
.
C
:
err
=
wsjson
.
Write
(
ctx
,
c
,
"hi"
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
}
}
}
})
err
:=
http
.
ListenAndServe
(
"localhost:8080"
,
fn
)
log
.
Fatal
(
err
)
}
This diff is collapsed.
Click to expand it.
websocket.go
+
4
−
1
View file @
4357cbf9
...
@@ -21,6 +21,9 @@ import (
...
@@ -21,6 +21,9 @@ import (
// All methods may be called concurrently except for Reader, Read
// All methods may be called concurrently except for Reader, Read
// and SetReadLimit.
// and SetReadLimit.
//
//
// You must always read from the connection. Otherwise control
// frames will not be handled. See the docs on Reader.
//
// Please be sure to call Close on the connection when you
// Please be sure to call Close on the connection when you
// are finished with it to release the associated resources.
// are finished with it to release the associated resources.
type
Conn
struct
{
type
Conn
struct
{
...
@@ -299,7 +302,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
...
@@ -299,7 +302,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
// You must read from the connection for close frames to be read.
// 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
// If you do not expect any data messages from the peer, just call
// Reader in a separate goroutine and close the connection with StatusPolicyViolation
// Reader in a separate goroutine and close the connection with StatusPolicyViolation
// when it returns.
Example at // TODO
// when it returns.
See the WriteOnly example.
//
//
// Only one Reader may be open at a time.
// Only one Reader may be open at a time.
//
//
...
...
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