1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package user
- import (
- "context"
- "github.com/gorilla/websocket"
- "microGame/app/usercenter/cmd/api/internal/game"
- "net/http"
- "github.com/zeromicro/go-zero/core/logx"
- "microGame/app/usercenter/cmd/api/internal/svc"
- )
- var upgrade = websocket.Upgrader{
- ReadBufferSize: 1024,
- WriteBufferSize: 1024,
- CheckOrigin: func(r *http.Request) bool {
- return true
- },
- }
- type WsLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- // NewWsLogic ws
- func NewWsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WsLogic {
- return &WsLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- // Ws 程序退出关闭所有长连接
- func (l *WsLogic) Ws(w http.ResponseWriter, r *http.Request) {
- k := r.URL.Query().Get("k")
- if k == "" {
- return
- }
- conn, err := upgrade.Upgrade(w, r, nil)
- if err != nil {
- return
- }
- client := &game.Player{
- SvcCtx: l.svcCtx,
- Conn: conn,
- Send: make(chan []byte, game.BufSize),
- //Uid: 100,
- }
- client.SvcCtx.Hub.Register <- client
- go client.WritePump()
- go client.ReadPump()
- }
|