websocket

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

websocket

This is a golang websocket client easy to use

Usage

import (
	"context"
	"io"

	logger "github.com/charmbracelet/log"
)

type Receiver struct {
	*logger.Logger
}

func (r *Receiver) OnReceive(frame *Frame) {
	bs, err := io.ReadAll(frame.Reader)
	if err != nil {
		r.Error("读取消息失败!", "err", err)
		return
	}
	r.Infof("收到消息: %s", bs)
}

// SetLogger is a Optional func with Processor
func (r *Receiver) SetLogger(l *logger.Logger) {
	r.Logger = l
}

type Message struct {
	*JsonMessage
	Name string
}

func main() {
	ctx := context.Background()
	client := NewClient(ctx, "ws://121.40.165.18:8800", &Receiver{}, WithPing(NewStringMessage("ping")))
	err := client.Connect()
	if err != nil {
		logger.Fatal(err)
	}

	client.Subscribe(&Message{
		Name: "Joe",
	})

	<-make(chan struct{})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AesDecryptECB

func AesDecryptECB(encrypted, key []byte) (decrypted []byte)

func AesEncryptECB

func AesEncryptECB(src []byte, key []byte) (encrypted []byte)

func AesSha1Prng

func AesSha1Prng(key []byte, length int) ([]byte, error)

func EventStrings added in v1.1.9

func EventStrings() []string

EventStrings returns a slice of all String values of the enum

func FrameTypeStrings

func FrameTypeStrings() []string

FrameTypeStrings returns a slice of all String values of the enum

func HexMD5

func HexMD5(src []byte) string

func HexSha1

func HexSha1(src []byte) (digest []byte)

func HmacMD5

func HmacMD5(src, secret []byte) string

func HmacSHA256

func HmacSHA256(src, secret []byte) string

func MD5

func MD5(src []byte) []byte

func Sha1

func Sha1(src []byte) []byte

func StatusStrings

func StatusStrings() []string

StatusStrings returns a slice of all String values of the enum

Types

type BeforeReconnectHandler added in v1.1.9

type BeforeReconnectHandler func(*Client)

type Client

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

func NewClient

func NewClient(ctx context.Context, URL string, receiver Receiver, opts ...Option[Client]) (*Client, error)

func (*Client) SendBinary

func (c *Client) SendBinary(message []byte)

func (*Client) SendClose

func (c *Client) SendClose(message []byte)

func (*Client) SendJson

func (c *Client) SendJson(message any) error

func (*Client) SendMessage

func (c *Client) SendMessage(message []byte)

func (*Client) SendPing

func (c *Client) SendPing(message []byte)

func (*Client) SendPong

func (c *Client) SendPong(message []byte)

func (*Client) Shutdown

func (c *Client) Shutdown() (err error)

func (*Client) Status added in v1.1.9

func (c *Client) Status() Status

func (*Client) URL added in v1.1.9

func (c *Client) URL() string

type DecompressHandler

type DecompressHandler func(io.Reader) (io.Reader, error)

type ErrorHandler

type ErrorHandler func(FrameType, Event, error)

type Event added in v1.1.9

type Event uint
const (
	EventNormal Event = iota
	EventRead
	EventWrite
	EventDecompress
	EventUnmarshal
	EventReconnect
)

func EventString added in v1.1.9

func EventString(s string) (Event, error)

EventString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func EventValues added in v1.1.9

func EventValues() []Event

EventValues returns all values of the enum

func (Event) IsAEvent added in v1.1.9

func (i Event) IsAEvent() bool

IsAEvent returns "true" if the value is listed in the enum definition. "false" otherwise

func (Event) MarshalJSON added in v1.1.9

func (i Event) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Event

func (Event) MarshalText added in v1.1.9

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

MarshalText implements the encoding.TextMarshaler interface for Event

func (Event) String added in v1.1.9

func (i Event) String() string

func (*Event) UnmarshalJSON added in v1.1.9

func (i *Event) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Event

func (*Event) UnmarshalText added in v1.1.9

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

UnmarshalText implements the encoding.TextUnmarshaler interface for Event

func (Event) Values added in v1.1.9

func (Event) Values() []string

type FrameType

type FrameType int
const (
	FrameTypeNoFrame FrameType = iota - 1
	FrameTypeText    FrameType = iota
	FrameTypeBinary
	FrameTypeClose FrameType = iota + 6
	FrameTypePing
	FrameTypePong
)

func FrameTypeString

func FrameTypeString(s string) (FrameType, error)

FrameTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func FrameTypeValues

func FrameTypeValues() []FrameType

FrameTypeValues returns all values of the enum

func (FrameType) IsAFrameType

func (i FrameType) IsAFrameType() bool

IsAFrameType returns "true" if the value is listed in the enum definition. "false" otherwise

func (FrameType) MarshalJSON

func (i FrameType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for FrameType

func (FrameType) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface for FrameType

func (FrameType) String

func (i FrameType) String() string

func (*FrameType) UnmarshalJSON

func (i *FrameType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for FrameType

func (*FrameType) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface for FrameType

func (FrameType) Values

func (FrameType) Values() []string

type HeartbeatHandler

type HeartbeatHandler func(*Client)

type Logger added in v1.1.9

type Logger interface {
	Info(string, ...any)
	Error(string, ...any)
}

type Message

type Message struct {
	Type FrameType
	Msg  []byte
}

type OnCloseHandler

type OnCloseHandler func(code int, text string) error

type OnConnectedHandler

type OnConnectedHandler func(*Client)

type Option

type Option[T any] func(*T)

func WithAutoReConnect

func WithAutoReConnect() Option[Client]

func WithBeforeReconnect added in v1.1.9

func WithBeforeReconnect(handler BeforeReconnectHandler) Option[Client]

func WithCloseHandler

func WithCloseHandler(handler OnCloseHandler) Option[Client]

func WithCompression

func WithCompression(compression bool) Option[Client]

func WithCompressionLevel

func WithCompressionLevel(level int) Option[Client]

func WithConnectHeader added in v1.2.1

func WithConnectHeader(header http.Header) Option[Client]

func WithConnectTimeout

func WithConnectTimeout(timeout time.Duration) Option[Client]

func WithConnected

func WithConnected(handler OnConnectedHandler) Option[Client]

func WithDecompressHandler

func WithDecompressHandler(handler DecompressHandler) Option[Client]

func WithDelayReconnect added in v1.2.1

func WithDelayReconnect(delay time.Duration) Option[Client]

func WithDialer

func WithDialer(dialer websocket.Dialer) Option[Client]

func WithErrorHandler

func WithErrorHandler(handler ErrorHandler) Option[Client]

func WithHeartbeatHandler

func WithHeartbeatHandler(handler HeartbeatHandler) Option[Client]

func WithHeartbeatInterval

func WithHeartbeatInterval(interval time.Duration) Option[Client]

func WithLogger

func WithLogger(logger *slog.Logger) Option[Client]

WithLogger custom specify logger instance

func WithPingHandler

func WithPingHandler(handler PingHandler) Option[Client]

func WithPongHandler

func WithPongHandler(handler PongHandler) Option[Client]

func WithProxyURL

func WithProxyURL(proxyURL string) Option[Client]

func WithReadLimit

func WithReadLimit(limit int64) Option[Client]

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) Option[Client]

func WithReconnected

func WithReconnected(handler ReConnectedHandler) Option[Client]

type PingHandler

type PingHandler func(appData string) error

type PongHandler

type PongHandler func(appData string) error

type ReConnectedHandler

type ReConnectedHandler func(*Client)

type ReaderReceiver added in v1.1.9

type ReaderReceiver struct{}

func (ReaderReceiver) Unmarshal added in v1.1.9

func (ReaderReceiver) Unmarshal(_ FrameType, reader io.Reader) (any, error)

type Receiver

type Receiver interface {
	Unmarshaler
	OnMessage(any)
}

type Status

type Status uint32
const (
	StatusDisconnected Status = iota
	StatusConnecting
	StatusDisconnecting
	StatusEstablish // unused
	StatusInactive  // unused
	StatusConnected
	StatusReConnecting
)

func StatusString

func StatusString(s string) (Status, error)

StatusString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func StatusValues

func StatusValues() []Status

StatusValues returns all values of the enum

func (Status) IsAStatus

func (i Status) IsAStatus() bool

IsAStatus returns "true" if the value is listed in the enum definition. "false" otherwise

func (Status) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface for Status

func (Status) String

func (i Status) String() string

func (*Status) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface for Status

func (Status) Values

func (Status) Values() []string

type Unmarshaler added in v1.1.9

type Unmarshaler interface {
	Unmarshal(frameType FrameType, reader io.Reader) (any, error)
}

Jump to

Keyboard shortcuts

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