Files
mayfly-go/server/pkg/ws/ws.go

42 lines
760 B
Go
Raw Normal View History

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() // 管理者
func init() {
2023-09-12 20:54:07 +08:00
go Manager.Start()
}
2023-09-12 20:54:07 +08:00
// 添加ws客户端
func AddClient(userId UserId, clientId string, conn *websocket.Conn) *Client {
if len(clientId) == 0 {
return nil
}
cli := NewClient(UserId(userId), clientId, conn)
cli.Read()
2023-09-12 20:54:07 +08:00
Manager.AddClient(cli)
return cli
}
func CloseClient(uid UserId) {
Manager.CloseByUid(uid)
}
2023-10-10 17:39:46 +08:00
// 对指定用户发送json类型消息
func SendJsonMsg(userId UserId, clientId string, msg any) {
Manager.SendJsonMsg(userId, clientId, msg)
}