-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement websocket keepalive #64
Conversation
6d7b81c
to
d9fc4c1
Compare
d9fc4c1
to
b86dad9
Compare
3fe558a
to
a32f138
Compare
acceptor.go
Outdated
@@ -69,10 +70,11 @@ func (w *WebSocketAcceptor) Spec(subProtocol string) (serializers.Serializer, er | |||
return serializer, nil | |||
} | |||
|
|||
func (w *WebSocketAcceptor) Accept(conn net.Conn) (BaseSession, error) { | |||
func (w *WebSocketAcceptor) Accept(conn net.Conn, keepaliveInterval time.Duration, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two parameters should be moved into WebSocketServerConfig
and Accept should accept that.
acceptor_test.go
Outdated
@@ -25,7 +26,7 @@ func TestAccept(t *testing.T) { | |||
require.NotNil(t, conn) | |||
|
|||
acceptor := xconn.WebSocketAcceptor{} | |||
session, err := acceptor.Accept(conn) | |||
session, err := acceptor.Accept(conn, time.Duration(0), time.Duration(0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would then just become
session, err := acceptor.Accept(conn, time.Duration(0), time.Duration(0)) | |
session, err := acceptor.Accept(conn, nil) |
client.go
Outdated
KeepaliveInterval time.Duration | ||
KeepaliveTimeout time.Duration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KeepaliveInterval time.Duration | |
KeepaliveTimeout time.Duration | |
KeepAliveInterval time.Duration | |
KeepAliveTimeout time.Duration |
joiner.go
Outdated
@@ -22,7 +22,8 @@ type WebSocketJoiner struct { | |||
DialTimeout time.Duration | |||
} | |||
|
|||
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string) (BaseSession, error) { | |||
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, keepaliveInterval time.Duration, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those new parameters should be moved inside WSDialerConfig struct. And this function should signature should that as last argument
peer.go
Outdated
wsReader, wsWriter, err = ServerSideWSReaderWriter(binary) | ||
} else { | ||
wsReader, wsWriter, err = ClientSideWSReaderWriter(binary) | ||
func NewWebSocketPeer(conn net.Conn, protocol string, binary, server bool, keepaliveInterval time.Duration, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create a new WSPeerConfig struct and move all parameters of this function there (except for conn)
peer.go
Outdated
} | ||
|
||
if err != nil { | ||
return nil, err | ||
if keepaliveInterval != 0*time.Second { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if keepaliveInterval != 0*time.Second { | |
if keepaliveInterval != 0 { |
peer.go
Outdated
ticker := time.NewTicker(keepaliveInterval) | ||
defer ticker.Stop() | ||
|
||
if keepaliveTimeout == 0*time.Second { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if keepaliveTimeout == 0*time.Second { | |
if keepaliveTimeout == 0 { |
peer.go
Outdated
select { | ||
case <-c.pingCh: | ||
case <-time.After(keepaliveTimeout): | ||
log.Println("pong timeout, closing connection") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Println("pong timeout, closing connection") | |
log.Println("ping timeout, closing connection") |
peer.go
Outdated
case ws.OpText, ws.OpBinary: | ||
return payload, nil | ||
case ws.OpPing: | ||
err = c.writeOpFunc(c.conn, ws.OpPong, payload) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use if in this line
server.go
Outdated
} | ||
|
||
func NewServer(router *Router, authenticator auth.ServerAuthenticator, throttle *internal.Throttle) *Server { | ||
func NewServer(router *Router, authenticator auth.ServerAuthenticator, throttle *internal.Throttle, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a new ServerConfig struct
a32f138
to
ba42f2f
Compare
ba42f2f
to
8561681
Compare
joiner.go
Outdated
} | ||
|
||
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string) (BaseSession, error) { | ||
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, dialConfig WSDialerConfig) (BaseSession, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, dialConfig WSDialerConfig) (BaseSession, error) { | |
func (w *WebSocketJoiner) Join(ctx context.Context, url, realm string, config *WSDialerConfig) (BaseSession, error) { |
we should also change DialWebSocket to accept pointer of config
8561681
to
d685e03
Compare
d685e03
to
3c8a518
Compare
peer.go
Outdated
for { | ||
<-ticker.C | ||
// Send a ping | ||
err := c.writeOpFunc(c.conn, ws.OpPing, []byte("ping")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of sending "ping" please send 4 random bytes
No description provided.