mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
feat: 前端用户信息迁移至localstorage
This commit is contained in:
@@ -23,9 +23,28 @@ type Client struct {
|
||||
ReadMsgHander ReadMsgHandlerFunc // 读取消息处理函数
|
||||
}
|
||||
|
||||
func NewClient(userId UserId, socket *websocket.Conn) *Client {
|
||||
cli := &Client{
|
||||
ClientId: stringx.Rand(16),
|
||||
UserId: userId,
|
||||
WsConn: socket,
|
||||
}
|
||||
|
||||
return cli
|
||||
}
|
||||
|
||||
func (c *Client) WithReadHandlerFunc(readMsgHandlerFunc ReadMsgHandlerFunc) *Client {
|
||||
c.ReadMsgHander = readMsgHandlerFunc
|
||||
return c
|
||||
}
|
||||
|
||||
// 读取ws客户端消息
|
||||
func (c *Client) Read() {
|
||||
go func() {
|
||||
for {
|
||||
if c.WsConn == nil {
|
||||
return
|
||||
}
|
||||
messageType, data, err := c.WsConn.ReadMessage()
|
||||
if err != nil {
|
||||
if messageType == -1 && websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure, websocket.CloseNoStatusReceived) {
|
||||
@@ -72,13 +91,3 @@ func (c *Client) WriteMsg(msg *Msg) error {
|
||||
func (c *Client) Ping() error {
|
||||
return c.WsConn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
||||
}
|
||||
|
||||
func NewClient(userId UserId, socket *websocket.Conn) *Client {
|
||||
cli := &Client{
|
||||
ClientId: stringx.Rand(16),
|
||||
UserId: userId,
|
||||
WsConn: socket,
|
||||
}
|
||||
|
||||
return cli
|
||||
}
|
||||
|
||||
@@ -141,8 +141,10 @@ func (manager *ClientManager) doConnect(client *Client) {
|
||||
// 处理断开连接
|
||||
func (manager *ClientManager) doDisconnect(client *Client) {
|
||||
//关闭连接
|
||||
_ = client.WsConn.Close()
|
||||
client.WsConn = nil
|
||||
if client.WsConn != nil {
|
||||
_ = client.WsConn.Close()
|
||||
client.WsConn = nil
|
||||
}
|
||||
manager.delClient4Map(client)
|
||||
logx.Debugf("WS客户端已断开: uid=%d, count=%d", client.UserId, Manager.Count())
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ func init() {
|
||||
// 添加ws客户端
|
||||
func AddClient(userId uint64, conn *websocket.Conn) *Client {
|
||||
cli := NewClient(UserId(userId), conn)
|
||||
cli.Read()
|
||||
Manager.AddClient(cli)
|
||||
return cli
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user