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
711cce47
Unverified
Commit
711cce47
authored
5 years ago
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Add msgType parameter to NetConn adapter
Closes #113
parent
5028f225
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
netconn.go
+15
-9
15 additions, 9 deletions
netconn.go
websocket_test.go
+2
-2
2 additions, 2 deletions
websocket_test.go
with
17 additions
and
11 deletions
netconn.go
+
15
−
9
View file @
711cce47
...
...
@@ -2,6 +2,7 @@ package websocket
import
(
"context"
"fmt"
"io"
"math"
"net"
...
...
@@ -17,8 +18,11 @@ import (
// correctly and so provided in the library.
// See https://github.com/nhooyr/websocket/issues/100.
//
// Every Write to the net.Conn will correspond to a binary message
// write on *webscoket.Conn.
// Every Write to the net.Conn will correspond to a message write of
// the given type on *websocket.Conn.
//
// If a message is read that is not of the correct type, an error
// will be thrown.
//
// Close will close the *websocket.Conn with StatusNormalClosure.
//
...
...
@@ -30,9 +34,10 @@ import (
// and "websocket/unknown-addr" for String.
//
// A received StatusNormalClosure close frame will be translated to EOF when reading.
func
NetConn
(
c
*
Conn
)
net
.
Conn
{
func
NetConn
(
c
*
Conn
,
msgType
MessageType
)
net
.
Conn
{
nc
:=
&
netConn
{
c
:
c
,
c
:
c
,
msgType
:
msgType
,
}
var
cancel
context
.
CancelFunc
...
...
@@ -52,7 +57,8 @@ func NetConn(c *Conn) net.Conn {
}
type
netConn
struct
{
c
*
Conn
c
*
Conn
msgType
MessageType
writeTimer
*
time
.
Timer
writeContext
context
.
Context
...
...
@@ -71,7 +77,7 @@ func (c *netConn) Close() error {
}
func
(
c
*
netConn
)
Write
(
p
[]
byte
)
(
int
,
error
)
{
err
:=
c
.
c
.
Write
(
c
.
writeContext
,
MessageBinary
,
p
)
err
:=
c
.
c
.
Write
(
c
.
writeContext
,
c
.
msgType
,
p
)
if
err
!=
nil
{
return
0
,
err
}
...
...
@@ -93,9 +99,9 @@ func (c *netConn) Read(p []byte) (int, error) {
}
return
0
,
err
}
if
typ
!=
MessageBinary
{
c
.
c
.
Close
(
StatusUnsupportedData
,
"can only accept
binary
messages"
)
return
0
,
xerrors
.
Errorf
(
"unexpected frame type read for net conn adapter (expected %v): %v"
,
MessageBinary
,
typ
)
if
typ
!=
c
.
msgType
{
c
.
c
.
Close
(
StatusUnsupportedData
,
fmt
.
Sprintf
(
"can only accept
%v
messages"
,
c
.
msgType
)
)
return
0
,
xerrors
.
Errorf
(
"unexpected frame type read for net conn adapter (expected %v): %v"
,
c
.
msgType
,
typ
)
}
c
.
reader
=
r
}
...
...
This diff is collapsed.
Click to expand it.
websocket_test.go
+
2
−
2
View file @
711cce47
...
...
@@ -127,7 +127,7 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
nc
:=
websocket
.
NetConn
(
c
)
nc
:=
websocket
.
NetConn
(
c
,
websocket
.
MessageBinary
)
defer
nc
.
Close
()
nc
.
SetWriteDeadline
(
time
.
Time
{})
...
...
@@ -152,7 +152,7 @@ func TestHandshake(t *testing.T) {
}
defer
c
.
Close
(
websocket
.
StatusInternalError
,
""
)
nc
:=
websocket
.
NetConn
(
c
)
nc
:=
websocket
.
NetConn
(
c
,
websocket
.
MessageBinary
)
defer
nc
.
Close
()
nc
.
SetReadDeadline
(
time
.
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