websocketPool

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ClientPoolApp     ClientPool
	ClientInstanceApp ClientInstance

	HeartOpt          Heart
	MessageTimeoutOpt MessageTimeout
)
View Source
var (
	ServerPoolApp    ServerPool
	SeverInstanceApp ServerInstance
)
View Source
var MsgType msgType

Functions

func ClientDemo

func ClientDemo()

func ServerDemo

func ServerDemo()

Types

type Client

type Client struct {
	InstanceName, Name string
	Conn               *websocket.Conn
	// contains filtered or unexported fields
}

Client websocket 客户端链接

func NewClient

func NewClient(instanceName, name, host, path string, receiveMessageFunc func(instanceName, clientName string, prototypeMsg []byte) ([]byte, error)) (*Client, error)

NewClient 实例化:websocket 客户端链接

func (*Client) Close

func (my *Client) Close() error

Close 关闭链接

func (*Client) SendMsg

func (my *Client) SendMsg(msgType int, msg []byte) ([]byte, error)

SendMsg 发送消息:通过链接

type ClientInstance

type ClientInstance struct {
	Name    string
	Clients *dict.AnyDict[string, *Client]
}

ClientInstance websocket 客户端链接实例

func (*ClientInstance) Close

func (my *ClientInstance) Close()

Close 关闭客户端实例

func (*ClientInstance) GetClient

func (my *ClientInstance) GetClient(clientName string) (*Client, bool)

GetClient 获取websocket客户端链接

func (ClientInstance) New

func (ClientInstance) New(instanceName string) *ClientInstance

New 实例化:websocket 客户端实例

func (*ClientInstance) SendMsgByName

func (my *ClientInstance) SendMsgByName(clientName string, msgType int, msg []byte) ([]byte, error)

SendMsgByName 发送消息:通过名称

func (*ClientInstance) SetClient

func (my *ClientInstance) SetClient(
	clientName, host, path string,
	receiveMessageFn func(instanceName, clientName string, propertyMessage []byte) ([]byte, error),
	heart *Heart,
	timeout *MessageTimeout,
) (*Client, error)

SetClient 创建新链接

type ClientPool

type ClientPool struct {
	Error error
	// contains filtered or unexported fields
}

ClientPool websocket 客户端连接池

func (ClientPool) Close

func (ClientPool) Close()

Close 关闭客户端实例池

func (ClientPool) CloseClient

func (ClientPool) CloseClient(instanceName, clientName string) error

CloseClient 关闭链接

func (ClientPool) GetClient

func (ClientPool) GetClient(instanceName, clientName string) *Client

GetClient 获取客户端链接

func (ClientPool) GetClientInstance

func (ClientPool) GetClientInstance(instanceName string) (*ClientInstance, bool)

GetClientInstance 获取链接实例

func (ClientPool) Once

func (ClientPool) Once() *ClientPool

Once 单例化:websocket 客户端连接池

func (ClientPool) SendMsgByName

func (ClientPool) SendMsgByName(instanceName, clientName string, msgType int, msg []byte) ([]byte, error)

SendMsgByName 发送消息:通过名称

func (ClientPool) SetClient

func (ClientPool) SetClient(
	instanceName,
	clientName,
	host,
	path string,
	receiveMessageFn func(instanceName, clientName string, prototypeMsg []byte) ([]byte, error),
	heart *Heart,
	timeout *MessageTimeout,
) (*Client, error)

SetClient 设置websocket客户端链接

func (ClientPool) SetClientInstance

func (ClientPool) SetClientInstance(instanceName string) (*ClientInstance, error)

SetClientInstance 设置实例链接

func (ClientPool) SetOnCloseClientErr

func (ClientPool) SetOnCloseClientErr(fn func(instanceName, clientName string, err error)) *ClientPool

SetOnCloseClientErr 设置回调:关闭客户端链接错

func (ClientPool) SetOnConnect

func (ClientPool) SetOnConnect(fn func(instanceName, clientName string)) *ClientPool

SetOnConnect 设置回调:成功创建链接

func (ClientPool) SetOnConnectErr

func (ClientPool) SetOnConnectErr(fn func(instanceName, clientName string, err error)) *ClientPool

SetOnConnectErr 设置回调:链接错误

func (ClientPool) SetOnReceiveMsgErr

func (ClientPool) SetOnReceiveMsgErr(fn func(instanceName, clientName string, propertyMessage []byte, err error)) *ClientPool

SetOnReceiveMsgErr 设置回调:接收消息错误

func (ClientPool) SetOnSendMsgErr

func (ClientPool) SetOnSendMsgErr(fn func(instanceName, clientName string, err error)) *ClientPool

SetOnSendMsgErr 设置回调:发送消息错误

type Heart

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

Heart 链接心跳

func (Heart) Default

func (Heart) Default() *Heart

Default 默认心跳:10秒

func (Heart) New

func (Heart) New() *Heart

New 实例化:链接心跳

func (*Heart) SetFn

func (my *Heart) SetFn(fn func(client *Client)) *Heart

SetFn 设置回调:定时器执行内容

func (*Heart) SetInterval

func (my *Heart) SetInterval(interval time.Duration) *Heart

SetInterval 设置定时器

func (*Heart) Stop

func (my *Heart) Stop() *Heart

Stop 停止定时器

type MessageTimeout

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

MessageTimeout 通信超时

func (MessageTimeout) Default

func (MessageTimeout) Default() *MessageTimeout

Default 默认消息超时:5秒

func (MessageTimeout) New

New 实例化:链接超时

func (*MessageTimeout) SetInterval

func (r *MessageTimeout) SetInterval(interval time.Duration) *MessageTimeout

SetInterval 设置定时器时间

type PendingRequest

type PendingRequest struct {
	Uuid uuid.UUID
	Chan chan []byte
	Done chan struct{}
	Err  error
}

PendingRequest 待处理请求

type ResponseWrt

type ResponseWrt struct{}

func (ResponseWrt) Header

func (ResponseWrt) Header() http.Header

func (ResponseWrt) Write

func (ResponseWrt) Write([]byte) (int, error)

func (ResponseWrt) WriteHeader

func (ResponseWrt) WriteHeader(statusCode int)

type Server

type Server struct {
	Conn *websocket.Conn
	// contains filtered or unexported fields
}

Server websocket服务端链接

type ServerInstance

type ServerInstance struct {
	Connections *array.AnyArray[*Server]
}

ServerInstance websocket服务端实例

func (ServerInstance) New

New 实例化:链接切片

type ServerPool

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

ServerPool websocket 服务端连接池

func (ServerPool) Close

func (ServerPool) Close()

Close 关闭连接池

func (ServerPool) Handle

func (ServerPool) Handle(
	writer http.ResponseWriter,
	req *http.Request,
	header http.Header,
	condition func() (string, bool),
)

Handle 消息处理

func (ServerPool) Once

func (ServerPool) Once() *ServerPool

Once 单例化:服务端连接池

func (ServerPool) RegisterRouter

func (ServerPool) RegisterRouter(routerKey string, fn func(ws *websocket.Conn)) *ServerPool

RegisterRouter 注册路由

func (ServerPool) SendMsgByAccountOpenId

func (ServerPool) SendMsgByAccountOpenId(accountOpenId string, message []byte) error

SendMsgByAccountOpenId 根据用户openId发送消息

func (ServerPool) SendMsgByWsConn

func (ServerPool) SendMsgByWsConn(ws *websocket.Conn, message []byte) error

SendMsgByWsConn 通过链接发送消息

func (ServerPool) SendMsgByWsManyConn

func (ServerPool) SendMsgByWsManyConn(servers *array.AnyArray[*Server], message []byte)

SendMsgByWsManyConn 通过链接切片发送消息

func (ServerPool) SetOnCloseConnErr

func (ServerPool) SetOnCloseConnErr(onCloseConnectionErr func(conn *websocket.Conn, err error)) *ServerPool

SetOnCloseConnErr 设置回调:关闭链接错误

func (ServerPool) SetOnConnect

func (ServerPool) SetOnConnect(onConnect func(*websocket.Conn)) *ServerPool

SetOnConnect 设置回调:链接成功后

func (ServerPool) SetOnConnectErr

func (ServerPool) SetOnConnectErr(onConnectErr func(error)) *ServerPool

SetOnConnectErr 设置回调:链接失败后

func (ServerPool) SetOnPing

func (ServerPool) SetOnPing(fn func(*websocket.Conn)) *ServerPool

SetOnPing 设置回调:ping

func (ServerPool) SetOnReceiveMsg

func (ServerPool) SetOnReceiveMsg(onMessage func(*websocket.Conn, []byte) string) *ServerPool

SetOnReceiveMsg 设置回调:接收消息

func (ServerPool) SetOnReceiveMsgErr

func (ServerPool) SetOnReceiveMsgErr(onMessageErr func(*websocket.Conn, error)) *ServerPool

SetOnReceiveMsgErr 设置回调:接收消息失败

func (ServerPool) SetOnRouterErr

func (ServerPool) SetOnRouterErr(onRouterErr func(*websocket.Conn, error)) *ServerPool

SetOnRouterErr 设置回调:路由解析失败

func (ServerPool) SetOnSendMsgErr

func (ServerPool) SetOnSendMsgErr(onSendMessageErr func(conn *websocket.Conn, err error)) *ServerPool

SetOnSendMsgErr 设置回调:发送消息失败

Jump to

Keyboard shortcuts

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