From 9447166ce59f84f36b408e0ec60a1f497e9e3d29 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: Fri, 23 Feb 2024 08:33:26 +0800 Subject: [PATCH] v1.4 --- client/client.go | 1 + server/server.go | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/client/client.go b/client/client.go index 7b71ce8..943e8a3 100644 --- a/client/client.go +++ b/client/client.go @@ -11,6 +11,7 @@ type Client struct { Uuid string `json:"uuid"` Score int `json:"score"` Time int `json:"time"` + IsOwner string `json:"isOwner"` UserIcon string `json:"userIcon"` Heart string `json:"heart"` Hub map[string]map[string]*Client `json:"-"` diff --git a/server/server.go b/server/server.go index 014d222..89e1ddd 100644 --- a/server/server.go +++ b/server/server.go @@ -5,7 +5,6 @@ import ( pkg2 "DifProject/pkg" "encoding/json" "fmt" - "github.com/gorilla/websocket" "io" "net/http" @@ -36,7 +35,6 @@ func getAuthentication(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") // 设置允许的请求头 w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") - body, err := io.ReadAll(r.Body) if err != nil { fmt.Println("read message error") @@ -143,30 +141,31 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { } 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("成功获取连接") //交换信息 - postPkg1 := pkg2.PostPkg{ - Type: "homeOwner", - Code: "200", - Data: *hub[pkg.Data.RoomId][uid1], - } - postPkg2 := pkg2.PostPkg{ - Type: "invitees", + //房主身份识别 + 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: pkg.Data, + Data: clients, } - err := conn2.WriteJSON(postPkg1) - if err != nil { - fmt.Println("writeJSON1 fail") - return - } - err = conn1.WriteJSON(postPkg2) + err := conn1.WriteJSON(postPkgs) + err = conn2.WriteJSON(postPkgs) if err != nil { - fmt.Println("writeJSON2 fail") + fmt.Println("write json error") return } @@ -182,6 +181,7 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { uid = key fmt.Println(111) } + fmt.Println(uid) conn := hub[pkg.Data.RoomId][uid].Conn err := conn.WriteJSON(pkg) @@ -189,12 +189,12 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { fmt.Println("change error") return } - //case pkg2.HeartBeat: - // //心跳检测 - // if pkg.Data.Heart == "ping" { - // fmt.Println(111) - // } - } + case pkg2.HeartBeat: + //心跳检测 + if pkg.Data.Heart == "ping" { + fmt.Println(111) + } + }git a } } @@ -258,5 +258,5 @@ func main() { http.HandleFunc("/list", postList) - http.ListenAndServe("0.0.0.0:8887", nil) + http.ListenAndServe("localhost:8887", nil) } -- Gitee