websocket

package
v0.3.18 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package websocket provides a cross-platform WebSocket client implementation. On native, it uses github.com/gorilla/websocket. On web, it uses the browser's built-in WebSocket support via syscall/js.

The API is consistent across platforms, so low level details are not exposed. Use Connect to make a Client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents a WebSocket client connection. You can use Connect to create a new Client.

func Connect

func Connect(url string) (*Client, error)

Connect connects to a WebSocket server and returns a Client.

func (*Client) Close

func (c *Client) Close() error

Close cleanly closes the WebSocket connection. It does not directly trigger Client.OnClose, but once the connection is closed, Client.OnMessage will trigger it.

func (*Client) OnClose

func (c *Client) OnClose(f func())

OnClose sets a callback function to be called when the connection is closed. This function can only be called once.

func (*Client) OnMessage

func (c *Client) OnMessage(f func(typ MessageTypes, msg []byte))

OnMessage sets a callback function to be called when a message is received. This function can only be called once.

func (*Client) Send

func (c *Client) Send(typ MessageTypes, msg []byte) error

Send sends a message to the WebSocket server with the given type and message.

type MessageTypes

type MessageTypes int32 //enums:enum

MessageTypes are the types of messages that can be sent or received.

const (

	// TextMessage is a text message (string).
	// Equivalent to [websocket.TextMessage].
	TextMessage MessageTypes = iota + 1

	// BinaryMessage is a binary data message ([]byte).
	// Equivalent to [websocket.BinaryMessage].
	BinaryMessage
)
const MessageTypesN MessageTypes = 3

MessageTypesN is the highest valid value for type MessageTypes, plus one.

func MessageTypesValues

func MessageTypesValues() []MessageTypes

MessageTypesValues returns all possible values for the type MessageTypes.

func (MessageTypes) Desc

func (i MessageTypes) Desc() string

Desc returns the description of the MessageTypes value.

func (MessageTypes) Int64

func (i MessageTypes) Int64() int64

Int64 returns the MessageTypes value as an int64.

func (MessageTypes) MarshalText

func (i MessageTypes) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*MessageTypes) SetInt64

func (i *MessageTypes) SetInt64(in int64)

SetInt64 sets the MessageTypes value from an int64.

func (*MessageTypes) SetString

func (i *MessageTypes) SetString(s string) error

SetString sets the MessageTypes value from its string representation, and returns an error if the string is invalid.

func (MessageTypes) String

func (i MessageTypes) String() string

String returns the string representation of this MessageTypes value.

func (*MessageTypes) UnmarshalText

func (i *MessageTypes) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (MessageTypes) Values

func (i MessageTypes) Values() []enums.Enum

Values returns all possible values for the type MessageTypes.

Directories

Path Synopsis
example
client command
server command

Jump to

Keyboard shortcuts

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