Skip to content

Commit

Permalink
websocket: add SessionWithContext, typo
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Oct 11, 2023
1 parent 207fd5a commit 7a56b23
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package websocket

import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -564,6 +565,11 @@ func (c *Conn) WriteMessage(messageType MessageType, data []byte) error {

// Session returns user session.
func (c *Conn) Session() interface{} {
return c.session
}

// SessionWithLock returns user session with lock, returns as soon as the session has been seted.
func (c *Conn) SessionWithLock() interface{} {
c.mux.Lock()
ch := c.chSessionInited
c.mux.Unlock()
Expand All @@ -573,8 +579,19 @@ func (c *Conn) Session() interface{} {
return c.session
}

// SessionWithLock returns user session with lock.
func (c *Conn) SessionWithLock() interface{} {
// SessionWithContext returns user session, returns as soon as the session has been seted or
// waits until the context is done.
func (c *Conn) SessionWithContext(ctx context.Context) interface{} {
c.mux.Lock()
ch := c.chSessionInited
c.mux.Unlock()
if ch != nil {
select {
case <-ch:
case <-ctx.Done():
}

}
return c.session
}

Expand Down

0 comments on commit 7a56b23

Please sign in to comment.