# szgame **Repository Path**: oine/szgame ## Basic Information - **Project Name**: szgame - **Description**: golang websocket 通信 测试 单机游戏服务框架 - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-03 - **Last Updated**: 2024-01-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### szgame 对websocket的包装 ```go package main import ( "context" "github.com/gorilla/websocket" "os" "os/signal" "syscall" "szgame" "time" ) type Client struct { Conn *szgame.Connection AuthAt time.Time } func (c *Client) GetConn() *szgame.Connection { return c.Conn } func main() { svc := szgame.NewServer[*Client]("", "", 80, szgame.OnConnect{ OnNew: func(conn *szgame.Connection) { println("有新连接" + conn.WsConn.RemoteAddr().String()) }, OnClose: func(conn *szgame.Connection) { println("连接退出" + conn.WsConn.RemoteAddr().String()) }, }, szgame.OnClient[*Client]{ OnLogin: func(conn *szgame.Connection, msg []byte) (*Client, error) { err := conn.WsConn.WriteMessage(websocket.TextMessage, []byte(`{"notice":"成功"}`)) return &Client{Conn: conn, AuthAt: time.Now()}, err }, OnMsg: func(client *Client, msg []byte) { println("获得消息" + string(msg)) return }, OnNew: func(client *Client) { println("新客户到来") return }, OnClose: func(client *Client) { println("closed 客户离开") }, }, 1000000) endFunc := svc.BeginWsServer(context.Background()) signChan := make(chan os.Signal, 1) signal.Notify(signChan, syscall.SIGTERM, syscall.SIGINT) <-signChan endFunc() } ``` websocket: "github.com/gorilla/websocket"