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
github
nhooyr
websocket
Commits
0d59d7f5
Unverified
Commit
0d59d7f5
authored
5 years ago
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Simplify the examples
parent
d83ea559
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
+3
-8
3 additions, 8 deletions
README.md
accept.go
+3
-4
3 additions, 4 deletions
accept.go
example_test.go
+5
-14
5 additions, 14 deletions
example_test.go
websocket.go
+1
-1
1 addition, 1 deletion
websocket.go
websocket_test.go
+11
-11
11 additions, 11 deletions
websocket_test.go
with
23 additions
and
38 deletions
README.md
+
3
−
8
View file @
0d59d7f5
...
...
@@ -36,9 +36,7 @@ go get nhooyr.io/websocket
```
go
fn
:=
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
AcceptSubprotocols
(
"test"
),
)
c
,
err
:=
websocket
.
Accept
(
w
,
r
)
if
err
!=
nil
{
log
.
Printf
(
"server handshake failed: %v"
,
err
)
return
...
...
@@ -76,13 +74,10 @@ For a production quality example that shows off the low level API, see the [echo
### Client
```
go
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Minute
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Minute
)
defer
cancel
()
c
,
_
,
err
:=
websocket
.
Dial
(
ctx
,
"ws://localhost:8080"
,
websocket
.
DialSubprotocols
(
"test"
),
)
c
,
_
,
err
:=
websocket
.
Dial
(
ctx
,
"ws://localhost:8080"
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to ws dial: %v"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
accept.go
+
3
−
4
View file @
0d59d7f5
...
...
@@ -22,11 +22,10 @@ type acceptSubprotocols []string
func
(
o
acceptSubprotocols
)
acceptOption
()
{}
// AcceptSubprotocols list the subprotocols that Accept will negotiate with a client.
// The first protocol that a client supports will be negotiated.
// AcceptProtocols lists the websocket protocols that Accept will negotiate with a client.
// The empty protocol will always be negotiated as per RFC 6455. If you would like to
// reject it, close the connection i
s
c.Subprotocol() == "".
func
Accept
Subp
rotocols
(
subprotocols
...
string
)
AcceptOption
{
// reject it, close the connection i
f
c.Subprotocol() == "".
func
Accept
P
rotocols
(
subprotocols
...
string
)
AcceptOption
{
return
acceptSubprotocols
(
subprotocols
)
}
...
...
This diff is collapsed.
Click to expand it.
example_test.go
+
5
−
14
View file @
0d59d7f5
...
...
@@ -14,19 +14,15 @@ import (
func
ExampleAccept_echo
()
{
fn
:=
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
AcceptSubprotocols
(
"echo"
),
)
c
,
err
:=
websocket
.
Accept
(
w
,
r
)
if
err
!=
nil
{
log
.
Printf
(
"server handshake failed: %v"
,
err
)
return
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
ctx
:=
context
.
Background
()
echo
:=
func
()
error
{
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Minute
)
ctx
,
cancel
:=
context
.
WithTimeout
(
r
.
Context
()
,
time
.
Minute
)
defer
cancel
()
typ
,
r
,
err
:=
c
.
Read
(
ctx
)
...
...
@@ -70,9 +66,7 @@ func ExampleAccept_echo() {
func
ExampleAccept
()
{
fn
:=
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
AcceptSubprotocols
(
"test"
),
)
c
,
err
:=
websocket
.
Accept
(
w
,
r
)
if
err
!=
nil
{
log
.
Printf
(
"server handshake failed: %v"
,
err
)
return
...
...
@@ -106,13 +100,10 @@ func ExampleAccept() {
}
func
ExampleDial
()
{
ctx
:=
context
.
Background
()
ctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
time
.
Minute
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Minute
)
defer
cancel
()
c
,
_
,
err
:=
websocket
.
Dial
(
ctx
,
"ws://localhost:8080"
,
websocket
.
DialSubprotocols
(
"test"
),
)
c
,
_
,
err
:=
websocket
.
Dial
(
ctx
,
"ws://localhost:8080"
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to ws dial: %v"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
websocket.go
+
1
−
1
View file @
0d59d7f5
...
...
@@ -70,7 +70,7 @@ func (c *Conn) close(err error) {
// Subprotocol returns the negotiated subprotocol.
// An empty string means the default protocol.
func
(
c
*
Conn
)
Subp
rotocol
()
string
{
func
(
c
*
Conn
)
P
rotocol
()
string
{
return
c
.
subprotocol
}
...
...
This diff is collapsed.
Click to expand it.
websocket_test.go
+
11
−
11
View file @
0d59d7f5
...
...
@@ -34,7 +34,7 @@ func TestHandshake(t *testing.T) {
{
name
:
"handshake"
,
server
:
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
error
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
Accept
Subp
rotocols
(
"myproto"
))
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
Accept
P
rotocols
(
"myproto"
))
if
err
!=
nil
{
return
err
}
...
...
@@ -74,8 +74,8 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
if
c
.
Subp
rotocol
()
!=
""
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %v"
,
c
.
Subp
rotocol
())
if
c
.
P
rotocol
()
!=
""
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %v"
,
c
.
P
rotocol
())
}
return
nil
},
...
...
@@ -86,8 +86,8 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
if
c
.
Subp
rotocol
()
!=
""
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %v"
,
c
.
Subp
rotocol
())
if
c
.
P
rotocol
()
!=
""
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %v"
,
c
.
P
rotocol
())
}
return
nil
},
...
...
@@ -95,14 +95,14 @@ func TestHandshake(t *testing.T) {
{
name
:
"subprotocol"
,
server
:
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
error
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
Accept
Subp
rotocols
(
"echo"
,
"lar"
))
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
Accept
P
rotocols
(
"echo"
,
"lar"
))
if
err
!=
nil
{
return
err
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
if
c
.
Subp
rotocol
()
!=
"echo"
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %q"
,
c
.
Subp
rotocol
())
if
c
.
P
rotocol
()
!=
"echo"
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %q"
,
c
.
P
rotocol
())
}
return
nil
},
...
...
@@ -113,8 +113,8 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
if
c
.
Subp
rotocol
()
!=
"echo"
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %q"
,
c
.
Subp
rotocol
())
if
c
.
P
rotocol
()
!=
"echo"
{
return
xerrors
.
Errorf
(
"unexpected subprotocol: %q"
,
c
.
P
rotocol
())
}
return
nil
},
...
...
@@ -266,7 +266,7 @@ func TestAutobahnServer(t *testing.T) {
s
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
websocket
.
Accept
Subp
rotocols
(
"echo"
),
websocket
.
Accept
P
rotocols
(
"echo"
),
)
if
err
!=
nil
{
t
.
Logf
(
"server handshake failed: %+v"
,
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