代码拉取完成,页面将自动刷新
package websocketPool
import (
"errors"
"fmt"
"gitee.com/jericho-yu/util/v2/dict"
)
type (
// ClientPool websocket 客户端连接池
ClientPool struct {
onConnect func(instanceName string, clientName string)
onConnectErr func(instanceName, clientName string, err error)
onSendMsgErr func(instanceName, clientName string, err error)
onCloseErr func(instanceName, clientName string, err error)
onReceiveMsgErr func(instanceName, clientName string, prototypeMsg []byte, err error)
clientInstances *dict.AnyDict[string, *ClientInstance]
Error error
}
)
// Once 单例化:websocket 客户端连接池
func (ClientPool) Once() *ClientPool {
clientPoolOnce.Do(func() {
clientPoolIns = &ClientPool{}
clientPoolIns.clientInstances = dict.MakeAnyDict[string, *ClientInstance]()
})
return clientPoolIns
}
// SetOnConnect 设置回调:成功创建链接
func (ClientPool) SetOnConnect(fn func(instanceName, clientName string)) *ClientPool {
clientPoolIns.onConnect = fn
return clientPoolIns
}
// SetOnConnectErr 设置回调:链接错误
func (ClientPool) SetOnConnectErr(fn func(instanceName, clientName string, err error)) *ClientPool {
clientPoolIns.onConnectErr = fn
return clientPoolIns
}
// SetOnCloseClientErr 设置回调:关闭客户端链接错
func (ClientPool) SetOnCloseClientErr(fn func(instanceName, clientName string, err error)) *ClientPool {
clientPoolIns.onCloseErr = fn
return clientPoolIns
}
// SetOnSendMsgErr 设置回调:发送消息错误
func (ClientPool) SetOnSendMsgErr(fn func(instanceName, clientName string, err error)) *ClientPool {
clientPoolIns.onSendMsgErr = fn
return clientPoolIns
}
// SetOnReceiveMsgErr 设置回调:接收消息错误
func (ClientPool) SetOnReceiveMsgErr(fn func(instanceName, clientName string, propertyMessage []byte, err error)) *ClientPool {
clientPoolIns.onReceiveMsgErr = fn
return clientPoolIns
}
// GetClientInstance 获取链接实例
func (ClientPool) GetClientInstance(instanceName string) (*ClientInstance, bool) {
return clientPoolIns.clientInstances.Get(instanceName)
}
// SetClientInstance 设置实例链接
func (ClientPool) SetClientInstance(instanceName string) (*ClientInstance, error) {
var (
clientInstance *ClientInstance
exist bool
)
_, exist = clientPoolIns.clientInstances.Get(instanceName)
if exist {
return nil, fmt.Errorf("创建实例失败:%s已经存在", instanceName)
}
clientInstance = ClientInstanceApp.New(instanceName)
clientPoolIns.clientInstances.Set(instanceName, clientInstance)
return clientInstance, nil
}
// GetClient 获取客户端链接
func (ClientPool) GetClient(instanceName, clientName string) *Client {
var (
clientInstance *ClientInstance
client *Client
exist bool
)
clientInstance, exist = clientPoolIns.clientInstances.Get(instanceName)
if !exist {
clientPoolIns.Error = fmt.Errorf("实例不存在:%s", instanceName)
return nil
}
client, exist = clientInstance.GetClient(clientName)
if !exist {
clientPoolIns.Error = fmt.Errorf("链接不存在:%s", clientName)
return nil
}
return client
}
// SetClient 设置websocket客户端链接
func (ClientPool) SetClient(
instanceName,
clientName,
host,
path string,
receiveMessageFn func(instanceName, clientName string, prototypeMsg []byte) ([]byte, error),
heart *Heart,
timeout *MessageTimeout,
) (*Client, error) {
var (
exist bool
clientInstance *ClientInstance
)
clientInstance, exist = clientPoolIns.clientInstances.Get(instanceName)
if !exist {
clientInstance = ClientInstanceApp.New(instanceName)
clientPoolIns.clientInstances.Set(instanceName, clientInstance)
}
return clientInstance.SetClient(clientName, host, path, receiveMessageFn, heart, timeout)
}
// SendMsgByName 发送消息:通过名称
func (ClientPool) SendMsgByName(instanceName, clientName string, msgType int, msg []byte) ([]byte, error) {
var (
exist bool
clientInstance *ClientInstance
)
clientInstance, exist = clientPoolIns.clientInstances.Get(instanceName)
if !exist {
if clientPoolIns.onSendMsgErr != nil {
clientPoolIns.onSendMsgErr(instanceName, clientName, errors.New("没有找到客户端实例"))
}
}
return clientInstance.SendMsgByName(clientName, msgType, msg)
}
// Close 关闭客户端实例池
func (ClientPool) Close() {
for _, clientInstance := range clientPoolIns.clientInstances.All() {
clientInstance.Close()
}
clientPoolIns.clientInstances.Clean()
}
// CloseClient 关闭链接
func (ClientPool) CloseClient(instanceName, clientName string) error {
var (
exist bool
clientInstance *ClientInstance
client *Client
)
clientInstance, exist = clientPoolIns.clientInstances.Get(instanceName)
if !exist {
clientPoolIns.onCloseErr(instanceName, clientName, errors.New("没有找到客户端实例"))
return errors.New("没有找到客户端实例")
}
client, exist = clientInstance.Clients.Get(clientName)
if !exist {
clientPoolIns.onCloseErr(instanceName, clientName, errors.New("没有找到客户端链接"))
return errors.New("没有找到客户端链接")
}
return client.Close()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。