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

39 lines
687 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 uint64, conn *websocket.Conn) *Client {
cli := NewClient(UserId(userId), conn)
cli.Read()
2023-09-12 20:54:07 +08:00
Manager.AddClient(cli)
return cli
}
2023-09-12 20:54:07 +08:00
func CloseClient(userid uint64) {
Manager.CloseByUid(UserId(userid))
}
2023-10-10 17:39:46 +08:00
// 对指定用户发送json类型消息
func SendJsonMsg(userId uint64, msg any) {
2023-09-12 20:54:07 +08:00
Manager.SendJsonMsg(UserId(userId), msg)
}