2021-11-11 15:56:02 +08:00
|
|
|
package ws
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var Upgrader = websocket.Upgrader{
|
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
|
WriteBufferSize: 1024 * 1024 * 10,
|
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 20:54:07 +08:00
|
|
|
var Manager = NewClientManager() // 管理者
|
2021-11-11 15:56:02 +08:00
|
|
|
|
2021-11-12 14:35:34 +08:00
|
|
|
func init() {
|
2023-09-12 20:54:07 +08:00
|
|
|
go Manager.Start()
|
2021-11-12 14:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-12 20:54:07 +08:00
|
|
|
// 添加ws客户端
|
2023-10-18 15:24:29 +08:00
|
|
|
func AddClient(userId uint64, clientUuid string, conn *websocket.Conn) *Client {
|
|
|
|
|
if len(clientUuid) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
cli := NewClient(UserId(userId), clientUuid, conn)
|
2023-09-16 17:07:48 +08:00
|
|
|
cli.Read()
|
2023-09-12 20:54:07 +08:00
|
|
|
Manager.AddClient(cli)
|
|
|
|
|
return cli
|
2021-11-12 14:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-18 15:24:29 +08:00
|
|
|
func CloseClient(clientUuid string) {
|
|
|
|
|
Manager.CloseByClientUuid(clientUuid)
|
2021-11-12 14:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-10 17:39:46 +08:00
|
|
|
// 对指定用户发送json类型消息
|
2023-10-18 15:24:29 +08:00
|
|
|
func SendJsonMsg(clientUuid string, msg any) {
|
|
|
|
|
Manager.SendJsonMsg(clientUuid, msg)
|
2021-11-11 15:56:02 +08:00
|
|
|
}
|