ws_ns

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WS_SIMUL_CONN_TOTAL_LIMIT_NAME = "ws/simul-connections"
	OPTION_DOES_NOT_EXIST_FMT      = "option '%s' does not exist"
)
View Source
const (
	DEFAULT_WS_SERVER_HANDSHAKE_TIMEOUT = 3 * time.Second
	DEFAULT_WS_SERVER_READ_BUFFER_SIZE  = 10_000
	DEFAULT_WS_SERVER_WRITE_BUFFER_SIZE = 10_000
	DEFAULT_MAX_WS_CONN_MSG_SIZE        = 100_000
	DEFAULT_MAX_IP_WS_CONNS             = 10

	WEBSOCKET_CLOSE_TASK_PER_GOROUTINE  = 10
	SERVER_SIDE_WEBSOCKET_CLOSE_TIMEOUT = 2 * time.Second
	WEBSOCKET_SERVER_CLOSE_TIMEOUT      = 3 * time.Second

	DEFAULT_WS_MESSAGE_TIMEOUT      = 10 * time.Second
	DEFAULT_WS_WAIT_MESSAGE_TIMEOUT = 30 * time.Second
	DEFAULT_WS_HANDSHAKE_TIMEOUT    = 20 * time.Second
)

Variables

View Source
var (
	ErrClosingOrClosedWebsocketConn = errors.New("closed or closing websocket connection")
	ErrAlreadyReadingAllMessages    = errors.New("already reading all messages")
)
View Source
var (
	ErrClosedWebsocketServer    = errors.New("closed websocket server")
	ErrTooManyWsConnectionsOnIp = errors.New("too many websocket connections on same ip")
)

Functions

func NewWebsocketNamespace

func NewWebsocketNamespace() *core.Namespace

Types

type WebsocketConnectParams

type WebsocketConnectParams struct {
	Ctx              *core.Context
	URL              core.URL
	Insecure         bool
	RequestHeader    http.Header
	MessageTimeout   time.Duration //if 0 defaults to DEFAULT_WS_MESSAGE_TIMEOUT
	HandshakeTimeout time.Duration //if 0 defaults to DEFAULT_WS_HANDSHAKE_TIMEOUT
}

type WebsocketConnection

type WebsocketConnection struct {
	// contains filtered or unexported fields
}

func WebsocketConnect

func WebsocketConnect(args WebsocketConnectParams) (*WebsocketConnection, error)

func (*WebsocketConnection) Close

func (conn *WebsocketConnection) Close() error

func (*WebsocketConnection) Equal

func (conn *WebsocketConnection) Equal(ctx *core.Context, other core.Value, alreadyCompared map[uintptr]uintptr, depth int) bool

func (*WebsocketConnection) GetGoMethod

func (conn *WebsocketConnection) GetGoMethod(name string) (*core.GoFunction, bool)

func (*WebsocketConnection) IsClosedOrClosing

func (conn *WebsocketConnection) IsClosedOrClosing() bool

func (*WebsocketConnection) IsMutable

func (conn *WebsocketConnection) IsMutable() bool

func (*WebsocketConnection) PrettyPrint

func (conn *WebsocketConnection) PrettyPrint(w *bufio.Writer, config *core.PrettyPrintConfig, depth int, parentIndentCount int)

func (*WebsocketConnection) Prop

func (conn *WebsocketConnection) Prop(ctx *core.Context, name string) core.Value

func (*WebsocketConnection) PropertyNames

func (*WebsocketConnection) PropertyNames(ctx *core.Context) []string

func (*WebsocketConnection) ReadMessage

func (conn *WebsocketConnection) ReadMessage(ctx *core.Context) (messageType WebsocketMessageType, p []byte, err error)

func (*WebsocketConnection) RemoteAddrWithPort

func (conn *WebsocketConnection) RemoteAddrWithPort() netaddr.RemoteAddrWithPort

func (*WebsocketConnection) SetPingHandler

func (conn *WebsocketConnection) SetPingHandler(ctx *core.Context, handler func(data string) error)

func (*WebsocketConnection) SetProp

func (*WebsocketConnection) SetProp(ctx *core.Context, name string, value core.Value) error

func (*WebsocketConnection) StartReadingAllMessagesIntoChan

func (conn *WebsocketConnection) StartReadingAllMessagesIntoChan(ctx *core.Context, channel chan WebsocketMessageChanItem) error

StartReadingAllMessagesIntoChan creates a goroutine that continuously calls ReadMessage() and puts results in channel. The goroutine stops when the context is done or the connection is closed or closing; the channel is closed. If the connection is already reading all messages ErrAlreadyReadingAllMessages is returned and the channel is not closed. If the connection is connection is closed ErrClosingOrClosedWebsocketConn is returned and the channel is not closed.

func (*WebsocketConnection) ToSymbolicValue

func (conn *WebsocketConnection) ToSymbolicValue(ctx *core.Context, encountered map[uintptr]symbolic.Value) (symbolic.Value, error)

func (*WebsocketConnection) WriteMessage

func (conn *WebsocketConnection) WriteMessage(ctx *core.Context, messageType WebsocketMessageType, data []byte) error

type WebsocketMessageChanItem

type WebsocketMessageChanItem struct {
	Error   error
	Type    WebsocketMessageType //nil if error
	Payload []byte               //nil if error
}

type WebsocketMessageType

type WebsocketMessageType int
const (
	WebsocketBinaryMessage WebsocketMessageType = websocket.BinaryMessage
	WebsocketTextMessage   WebsocketMessageType = websocket.TextMessage
	WebsocketPingMessage   WebsocketMessageType = websocket.PingMessage
	WebsocketPongMessage   WebsocketMessageType = websocket.PongMessage
	WebsocketCloseMessage  WebsocketMessageType = websocket.CloseMessage
)

type WebsocketServer

type WebsocketServer struct {
	// contains filtered or unexported fields
}

WebsocketServer upgrades an HTTP connection to a Websocket connection, it implements Value.

func NewWebsocketServer

func NewWebsocketServer(ctx *core.Context) (*WebsocketServer, error)

func (*WebsocketServer) Close

func (s *WebsocketServer) Close(ctx *core.Context) error

func (*WebsocketServer) Equal

func (s *WebsocketServer) Equal(ctx *core.Context, other core.Value, alreadyCompared map[uintptr]uintptr, depth int) bool

func (*WebsocketServer) GetGoMethod

func (s *WebsocketServer) GetGoMethod(name string) (*core.GoFunction, bool)

func (*WebsocketServer) IsMutable

func (s *WebsocketServer) IsMutable() bool

func (*WebsocketServer) PrettyPrint

func (s *WebsocketServer) PrettyPrint(w *bufio.Writer, config *core.PrettyPrintConfig, depth int, parentIndentCount int)

func (*WebsocketServer) Prop

func (s *WebsocketServer) Prop(ctx *core.Context, name string) core.Value

func (*WebsocketServer) PropertyNames

func (*WebsocketServer) PropertyNames(ctx *core.Context) []string

func (*WebsocketServer) SetProp

func (*WebsocketServer) SetProp(ctx *core.Context, name string, value core.Value) error

func (*WebsocketServer) ToSymbolicValue

func (s *WebsocketServer) ToSymbolicValue(ctx *core.Context, encountered map[uintptr]symbolic.Value) (symbolic.Value, error)

func (*WebsocketServer) Upgrade

func (*WebsocketServer) UpgradeGoValues

func (s *WebsocketServer) UpgradeGoValues(
	rw http.ResponseWriter,
	r *http.Request,
	allowConnectionFn func(remoteAddrPort netaddr.RemoteAddrWithPort, remoteAddr netaddr.RemoteIpAddr, currentConns []*WebsocketConnection) error,
) (*WebsocketConnection, error)

UpgradeGoValues first determines if the connection is allowed by calling allowConnectionFn, and then upgrades the HTTP server connection to the WebSocket protocol. If allowConnectionFn is nil the connection is accepted if the number of connections on the IP is less or equal to DEFAULT_MAX_IP_WS_CONNS. The execution of allowConnectionFn should be very quick because the server is locked during the UpgradeGoValues call.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL