From 1f470ef04d6428ce61535ae636bc5ba61c137718 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: Sat, 24 Feb 2024 11:41:00 +0800 Subject: [PATCH] v1.5 --- pkg/typePkg.go | 17 ++---- server/server.go | 144 +++++++++++++++++++++++++---------------------- 2 files changed, 84 insertions(+), 77 deletions(-) diff --git a/pkg/typePkg.go b/pkg/typePkg.go index 6380de6..d2a0599 100644 --- a/pkg/typePkg.go +++ b/pkg/typePkg.go @@ -28,17 +28,12 @@ type ListPkg struct { Data client2.Clients `json:"data"` } -//// PostHeartBeatPkg 心跳包 -//type PostHeartBeatPkg struct { -// Type string `json:"type"` -// Code string `json:"code"` -// Data string `json:"data"` -//} -// -//type HeartBeatPkg struct { -// Type string `json:"type"` -// Data string `json:"data"` -//} +// PostHeartBeatPkg 心跳包 + +type HeartBeatPkg struct { + Type string `json:"type"` + Data client2.Client `json:"data"` +} // Authentication 用户认证 var Authentication = "authentication" diff --git a/server/server.go b/server/server.go index 89e1ddd..2b1d1b2 100644 --- a/server/server.go +++ b/server/server.go @@ -22,6 +22,8 @@ var UP = websocket.Upgrader{ var hub = make(map[string]map[string]*client2.Client) +var tokens = make(map[string]struct{}) + var clients client2.Clients // 用户认证 @@ -65,6 +67,11 @@ func getAuthentication(w http.ResponseWriter, r *http.Request) { //用户认证 client.GetAuthentication() + if client.RoomId == "" { + client.IsOwner = "1" + } else { + client.IsOwner = "0" + } //返回数据 postPkg := pkg2.PostPkg{ Type: pkg2.Authentication, @@ -104,7 +111,7 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { } pkgType := pkg.Type - if pkgType == pkg2.BuildRoom || pkgType == pkg2.JoinRoom { + if pkgType == pkg2.BuildRoom { pkg.Data.Hub = hub pkg.Data.Conn = conn } @@ -113,63 +120,77 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { switch pkgType { //创建房间 case pkg2.BuildRoom: - pkg.Data.BuildRoom() - - postPkg := pkg2.PostPkg{ - Type: pkg2.BuildRoom, - Code: "200", - Data: pkg.Data, - } + if pkg.Data.IsOwner == "1" { + fmt.Println(111) + pkg.Data.BuildRoom() - err := pkg.Data.Conn.WriteJSON(postPkg) - if err != nil { - fmt.Println("build write error") - return - } - fmt.Println("build success") + //认证token + tokens[pkg.Data.RoomId] = struct{}{} - //加入房间 - case pkg2.JoinRoom: - 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 + postPkg := pkg2.PostPkg{ + Type: pkg2.BuildRoom, + Code: "200", + Data: pkg.Data, } - 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" - clients = append(clients, *hub[pkg.Data.RoomId][uid1]) - clients = append(clients, pkg.Data) - - postPkgs := pkg2.PostListPkg{ - Type: "changeInfo", - Code: "200", - Data: clients, + err := pkg.Data.Conn.WriteJSON(postPkg) + if err != nil { + fmt.Println("build write error") + return + } + 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 + } + 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" + clients = append(clients, *hub[pkg.Data.RoomId][uid1]) + clients = append(clients, pkg.Data) + + postPkgs := pkg2.PostListPkg{ + Type: "changeInfo", + Code: "200", + Data: clients, + } + + err := conn1.WriteJSON(postPkgs) + err = conn2.WriteJSON(postPkgs) + if err != nil { + fmt.Println("write json error") + return + } + + fmt.Println("join success") + } else { + fmt.Println("链接已失效") + } } - err := conn1.WriteJSON(postPkgs) - err = conn2.WriteJSON(postPkgs) - if err != nil { - fmt.Println("write json error") - return - } + //加入房间 + //case pkg2.JoinRoom: - fmt.Println("join success") //共享游戏进度 case pkg2.GameProgress: //转发分数 @@ -191,24 +212,15 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { } case pkg2.HeartBeat: //心跳检测 - if pkg.Data.Heart == "ping" { - fmt.Println(111) - } - }git a + pkg.Data.Heart = "pong" + } } } -// 心跳检测 -//func sendHeartbeat(conn *websocket.Conn) { -// for { -// err := conn.WriteMessage(websocket.TextMessage, []byte("pong")) -// if err != nil { -// log.Println("Error sending heartbeat:", err) -// return -// } -// time.Sleep(3 * time.Second) -// } -//} +func tokenAuth(token string) bool { + _, ok := tokens[token] + return ok +} func postList(w http.ResponseWriter, r *http.Request) { -- Gitee