diff --git a/client/client.go b/client/client.go index 7b71ce8a3c450715116f20be25d608eedde1909f..943e8a37594e765b73ae20df6f4be2217587532a 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 014d2220779cd3da8b9b2ac2b5ee903bb5e8169d..89e1ddde9bdf8e19ba76f1cbf290bd074c5b724c 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) }