From 66f1edd4cbc1a213071d5335aa0caeefe5761260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9C=8B=E7=B9=81=E6=98=9F?= <12936615+kanfanxing@user.noreply.gitee.com> Date: Tue, 12 Mar 2024 19:41:18 +0800 Subject: [PATCH] v2.1 --- pkg/typePkg.go | 12 --- server/server.go | 202 +++++++++++++++++++++++++---------------------- 2 files changed, 108 insertions(+), 106 deletions(-) diff --git a/pkg/typePkg.go b/pkg/typePkg.go index a3dc596..80c2efc 100644 --- a/pkg/typePkg.go +++ b/pkg/typePkg.go @@ -23,18 +23,6 @@ type PostListPkg struct { Data client2.Clients `json:"data"` } -type ListPkg struct { - Type string `json:"type"` - Data client2.Clients `json:"data"` -} - -// PostHeartBeatPkg 心跳包 - -type HeartBeatPkg struct { - Type string `json:"type"` - Data client2.Client `json:"data"` -} - type StartPkg struct { Type string `json:"type"` Code string `json:"code"` diff --git a/server/server.go b/server/server.go index 2cf275a..3435204 100644 --- a/server/server.go +++ b/server/server.go @@ -9,8 +9,6 @@ import ( "io" "net/http" "sort" - "strconv" - "time" ) var UP = websocket.Upgrader{ @@ -24,9 +22,9 @@ var UP = websocket.Upgrader{ var hub = make(map[string]map[string]*client2.Client) -var tokens = make(map[string]struct{}) +//var tokens = make(map[string][]string) -var clients client2.Clients +//var clients client2.Clients // 用户认证 func getAuthentication(w http.ResponseWriter, r *http.Request) { @@ -74,6 +72,21 @@ func getAuthentication(w http.ResponseWriter, r *http.Request) { } else { client.IsOwner = "0" } + + if client.IsOwner == "0" && len(hub[client.RoomId]) >= 2 { + postPkg := pkg2.PostPkg{ + Type: "joinError", + Code: "300", + } + m, err := json.Marshal(postPkg) + if err != nil { + //fmt.Println("jsonMarshal error") + return + } + w.Write(m) + return + } + //返回数据 postPkg := pkg2.PostPkg{ Type: pkg2.Authentication, @@ -86,19 +99,62 @@ func getAuthentication(w http.ResponseWriter, r *http.Request) { return } w.Write(m) - } -} -func sendHeartbeat(conn *websocket.Conn) { - time.Sleep(5 * time.Second) + case "exitRoom": + if pkg.Data.IsOwner == "0" { + fmt.Println("退出成功") + //将该用户从房间中退出 - for i := 0; i < 10; i++ { - // 模拟心跳消息 - err := conn.WriteJSON("ping" + strconv.Itoa(i)) - if err != nil { - fmt.Println(err) + var uid string + for key, _ := range hub[pkg.Data.RoomId] { + if key == pkg.Data.Uuid { + continue + } + uid = key + } + + postPkg := pkg2.PostPkg{ + Type: "otherExit", + Code: "200", + Data: pkg.Data, + } + fmt.Println("@@@", pkg.Data.RoomId) + hub[pkg.Data.RoomId][uid].Conn.WriteJSON(postPkg) + fmt.Println("发送成功") + + delete(hub[pkg.Data.RoomId], pkg.Data.Uuid) + + } else if pkg.Data.IsOwner == "1" { + fmt.Println("房主退出") + if len(hub[pkg.Data.RoomId]) == 1 { + //退出房间 + delete(hub[pkg.Data.RoomId], pkg.Data.Uuid) + + //注销房间 + delete(hub, pkg.Data.RoomId) + + } else if len(hub[pkg.Data.RoomId]) == 2 { + + var uid string + for key, _ := range hub[pkg.Data.RoomId] { + if key == pkg.Data.Uuid { + continue + } + uid = key + } + + postPkg1 := pkg2.PostPkg{ + Type: "exitRoom", + Code: "200", + Data: pkg.Data, + } + + hub[pkg.Data.RoomId][uid].Conn.WriteJSON(postPkg1) + + //注销房间 + delete(hub, pkg.Data.RoomId) + } } - time.Sleep(2 * time.Second) } } @@ -111,8 +167,6 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { return } - //go sendHeartbeat(conn) - for { //解析数据包 var pkg pkg2.Pkg @@ -120,7 +174,6 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { err := conn.ReadJSON(&pkg) fmt.Println(pkg) if err != nil { - //fmt.Println("") return } pkgType := pkg.Type @@ -132,13 +185,12 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { //处理请求 switch pkgType { - //创建房间 + //创建、加入房间 case pkg2.BuildRoom: if pkg.Data.IsOwner == "1" { pkg.Data.BuildRoom() - //认证token - tokens[pkg.Data.RoomId] = struct{}{} + //tokens[pkg.Data.RoomId] = append(tokens[pkg.Data.RoomId], pkg.Data.Uuid) postPkg := pkg2.PostPkg{ Type: pkg2.BuildRoom, @@ -148,69 +200,51 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { err := pkg.Data.Conn.WriteJSON(postPkg) if err != nil { - //fmt.Println("build write error") return } - //fmt.Println("build success") + fmt.Println("build success") } else { - if tokenAuth(pkg.Data.RoomId) { - delete(tokens, pkg.Data.RoomId) - pkg.Data.JoinRoom() - var conn1 *websocket.Conn - var conn2 *websocket.Conn - var uid1 string - //fmt.Println(hub[pkg.Data.RoomId]) - for id := range hub[pkg.Data.RoomId] { - if id == pkg.Data.Uuid { - continue - } - uid1 = id + pkg.Data.JoinRoom() + var conn1 *websocket.Conn + var conn2 *websocket.Conn + var uid1 string + + for id := range hub[pkg.Data.RoomId] { + if id == pkg.Data.Uuid { + continue } + uid1 = id + } - //fmt.Println(uid1) - - conn1 = hub[pkg.Data.RoomId][uid1].Conn - conn2 = hub[pkg.Data.RoomId][pkg.Data.Uuid].Conn - - //var clients []client2.Client - - //fmt.Println("成功获取连接") - //交换信息 - - //房主身份识别 - hub[pkg.Data.RoomId][uid1].IsOwner = "1" - pkg.Data.IsOwner = "0" - //fmt.Println(" 发送") - postPkg1 := pkg2.PostPkg{ - Type: "roomOwner", - Code: "200", - Data: *hub[pkg.Data.RoomId][uid1], - } + conn1 = hub[pkg.Data.RoomId][uid1].Conn + conn2 = hub[pkg.Data.RoomId][pkg.Data.Uuid].Conn - postPkg2 := pkg2.PostPkg{ - Type: "invitee", - Code: "200", - Data: pkg.Data, - } - err := conn1.WriteJSON(postPkg2) - if err != nil { - //fmt.Println("write json error") - return - } - err = conn2.WriteJSON(postPkg1) - if err != nil { - //fmt.Println("write json error") - return - } + postPkg1 := pkg2.PostPkg{ + Type: "roomOwner", + Code: "200", + Data: *hub[pkg.Data.RoomId][uid1], + } - fmt.Println("join success") - } else { - //fmt.Println("链接已失效") + postPkg2 := pkg2.PostPkg{ + Type: "invitee", + Code: "200", + Data: pkg.Data, + } + err := conn1.WriteJSON(postPkg2) + if err != nil { + return } + err = conn2.WriteJSON(postPkg1) + if err != nil { + return + } + + fmt.Println("join success") } + //开始游戏 case pkg2.StartGame: - fmt.Println("进来了") + var conn1 *websocket.Conn var conn2 *websocket.Conn @@ -325,16 +359,11 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { } case "check": //心跳检测 - //fmt.Println("check发送") conn.WriteJSON("check") + } } -} -// 令牌认证- -func tokenAuth(token string) bool { - _, ok := tokens[token] - return ok } func postList(w http.ResponseWriter, r *http.Request) { @@ -376,21 +405,6 @@ func postList(w http.ResponseWriter, r *http.Request) { uid = key } - //修正时间 - //if pkg.Data.Time == 0 || hub[pkg.Data.RoomId][uid].Time == 0 { - // if pkg.Data.Time == 0 { - // pkg.Data.Time = 60 - // } else if hub[pkg.Data.RoomId][uid].Time == 0 { - // hub[pkg.Data.RoomId][uid].Time = 60 - // } - //} else if pkg.Data.Time != 0 && hub[pkg.Data.RoomId][uid].Time != 0 { - // if pkg.Data.Time > hub[pkg.Data.RoomId][uid].Time { - // pkg.Data.Time = hub[pkg.Data.RoomId][uid].Time - // } else { - // hub[pkg.Data.RoomId][uid].Time = pkg.Data.Time - // } - //} - clients = append(clients, pkg.Data) clients = append(clients, *hub[pkg.Data.RoomId][uid]) @@ -417,5 +431,5 @@ func main() { http.HandleFunc("/list", postList) - http.ListenAndServe(":8887", nil) + http.ListenAndServe("localhost:8887", nil) } -- Gitee