Documentation
¶
Overview ¶
Package etp provides a client-server communication library using WebSockets. It supports event-based messaging, acknowledgments, and room-based broadcasting.
Index ¶
- Variables
- func IsNormalClose(err error) bool
- type AcceptOptions
- type Client
- func (c *Client) Close() error
- func (c *Client) Dial(ctx context.Context, url string) error
- func (c *Client) Emit(ctx context.Context, event string, data []byte) error
- func (c *Client) EmitWithAck(ctx context.Context, event string, data []byte) ([]byte, error)
- func (c *Client) On(event string, handler Handler) *Client
- func (c *Client) OnConnect(handler ConnectHandler) *Client
- func (c *Client) OnDisconnect(handler DisconnectHandler) *Client
- func (c *Client) OnError(handler ErrorHandler) *Client
- func (c *Client) OnUnknownEvent(handler Handler) *Client
- func (c *Client) Ping(ctx context.Context) error
- type ClientOption
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) Data() *store.Store
- func (c *Conn) Emit(ctx context.Context, event string, data []byte) error
- func (c *Conn) EmitWithAck(ctx context.Context, event string, data []byte) ([]byte, error)
- func (c *Conn) HttpRequest() *http.Request
- func (c *Conn) Id() string
- func (c *Conn) Ping(ctx context.Context) error
- type ConnectHandler
- type DialOptions
- type DisconnectHandler
- type ErrorHandler
- type Handler
- type HandlerFunc
- type Rooms
- func (s *Rooms) AllConns() []*Conn
- func (s *Rooms) Clear(rooms ...string)
- func (s *Rooms) Get(connId string) (*Conn, bool)
- func (s *Rooms) Join(conn *Conn, rooms ...string)
- func (s *Rooms) LeaveByConnId(id string, rooms ...string)
- func (s *Rooms) Len(room string) int
- func (s *Rooms) Rooms() []string
- func (s *Rooms) ToBroadcast(rooms ...string) []*Conn
- type Server
- func (s *Server) On(event string, handler Handler) *Server
- func (s *Server) OnConnect(handler ConnectHandler) *Server
- func (s *Server) OnDisconnect(handler DisconnectHandler) *Server
- func (s *Server) OnError(handler ErrorHandler) *Server
- func (s *Server) OnUnknownEvent(handler Handler) *Server
- func (s *Server) Rooms() *Rooms
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Shutdown()
- type ServerOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClientClosed is returned when the client is closed or not connected. ErrClientClosed = errors.New("client closed") )
Functions ¶
func IsNormalClose ¶
IsNormalClose checks if the error is a WebSocket normal closure.
Types ¶
type AcceptOptions ¶
type AcceptOptions = websocket.AcceptOptions
AcceptOptions is an alias for websocket.AcceptOptions.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a WebSocket client for event-based communication. It is safe for concurrent use.
func NewClient ¶
func NewClient(opts ...ClientOption) *Client
NewClient creates a new Client instance with the provided options.
func (*Client) Close ¶
Close closes the client connection. It returns ErrClientClosed if the client is already closed.
func (*Client) Dial ¶
Dial establishes a WebSocket connection to the specified URL. It returns an error if already connected or if the WebSocket dial fails.
func (*Client) Emit ¶
Emit sends an event with the specified data to the server. It returns ErrClientClosed if the client is not connected.
func (*Client) EmitWithAck ¶
EmitWithAck sends an event with the specified data to the server and waits for an acknowledgment. It returns ErrClientClosed if the client is not connected. The response data from the server is returned on success.
func (*Client) On ¶
On registers an event handler for the specified event name. It returns the Client to allow method chaining.
func (*Client) OnConnect ¶
func (c *Client) OnConnect(handler ConnectHandler) *Client
OnConnect registers a handler to be called when the client connects. It returns the Client to allow method chaining.
func (*Client) OnDisconnect ¶
func (c *Client) OnDisconnect(handler DisconnectHandler) *Client
OnDisconnect registers a handler to be called when the client disconnects. It returns the Client to allow method chaining.
func (*Client) OnError ¶
func (c *Client) OnError(handler ErrorHandler) *Client
OnError registers a handler to be called when an error occurs. It returns the Client to allow method chaining.
func (*Client) OnUnknownEvent ¶
OnUnknownEvent registers a handler to be called when an unknown event is received. It returns the Client to allow method chaining.
type ClientOption ¶
type ClientOption func(*clientOptions)
ClientOption is a function that configures a clientOptions struct.
func WithClientCodec ¶ added in v4.1.0
func WithClientCodec(codec msg.Codec) ClientOption
WithClientCodec sets the codec for encoding and decoding events.
func WithClientDialOptions ¶
func WithClientDialOptions(opts *DialOptions) ClientOption
WithClientDialOptions sets the WebSocket dial options for the client.
func WithClientReadLimit ¶
func WithClientReadLimit(limit int64) ClientOption
WithClientReadLimit sets the maximum message size in bytes that the client can read.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a WebSocket connection associated with a client or server.
func (*Conn) EmitWithAck ¶
EmitWithAck sends an event with the specified data and waits for an acknowledgment. It returns the response data from the receiver on success.
func (*Conn) HttpRequest ¶
HttpRequest returns the underlying HTTP request associated with the connection.
type ConnectHandler ¶
type ConnectHandler func(conn *Conn)
ConnectHandler is a function type called when a connection is established.
type DialOptions ¶
type DialOptions = websocket.DialOptions
DialOptions is an alias for websocket.DialOptions.
type DisconnectHandler ¶
DisconnectHandler is a function type called when a connection is closed.
type ErrorHandler ¶
ErrorHandler is a function type called when an error occurs.
type Handler ¶
Handler is an interface for handling events. The Handle method processes an event and returns a response.
type HandlerFunc ¶
HandlerFunc is a function type that implements the Handler interface.
type Rooms ¶
type Rooms struct {
// contains filtered or unexported fields
}
Rooms manages groups of connections for broadcasting. It is safe for concurrent use.
func (*Rooms) Get ¶
Get retrieves a connection by its ID from the internal room. It returns the connection and a boolean indicating whether it exists.
func (*Rooms) Join ¶
Join adds a connection to the specified rooms. New rooms are created if they do not exist.
func (*Rooms) LeaveByConnId ¶
LeaveByConnId removes a connection from the specified rooms. Empty rooms are automatically deleted.
func (*Rooms) ToBroadcast ¶
ToBroadcast returns all connections in the specified rooms for broadcasting. Duplicate connections are included multiple times if they belong to multiple rooms.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a WebSocket server for event-based communication. It is safe for concurrent use.
func NewServer ¶
func NewServer(opts ...ServerOption) *Server
NewServer creates a new Server instance with the provided options.
func (*Server) On ¶
On registers an event handler for the specified event name. It returns the Server to allow method chaining.
func (*Server) OnConnect ¶
func (s *Server) OnConnect(handler ConnectHandler) *Server
OnConnect registers a handler to be called when a client connects. It returns the Server to allow method chaining.
func (*Server) OnDisconnect ¶
func (s *Server) OnDisconnect(handler DisconnectHandler) *Server
OnDisconnect registers a handler to be called when a client disconnects. It returns the Server to allow method chaining.
func (*Server) OnError ¶
func (s *Server) OnError(handler ErrorHandler) *Server
OnError registers a handler to be called when an error occurs. It returns the Server to allow method chaining.
func (*Server) OnUnknownEvent ¶
OnUnknownEvent registers a handler to be called when an unknown event is received. It returns the Server to allow method chaining.
type ServerOption ¶
type ServerOption func(*serverOptions)
ServerOption is a function that configures a serverOptions struct.
func WithServerAcceptOptions ¶
func WithServerAcceptOptions(opts *AcceptOptions) ServerOption
WithServerAcceptOptions sets the WebSocket accept options for the server.
func WithServerCodec ¶ added in v4.1.0
func WithServerCodec(codec msg.Codec) ServerOption
WithServerCodec sets the codec for encoding and decoding events.
func WithServerReadLimit ¶
func WithServerReadLimit(limit int64) ServerOption
WithServerReadLimit sets the maximum message size in bytes that the server can read.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bpool provides a buffer pool for reusing *bytes.Buffer instances.
|
Package bpool provides a buffer pool for reusing *bytes.Buffer instances. |
|
Package msg provides message types and codecs for event-based communication.
|
Package msg provides message types and codecs for event-based communication. |
|
Package store provides a thread-safe key-value store for arbitrary data.
|
Package store provides a thread-safe key-value store for arbitrary data. |