From a57965c3a997dbc0fff7a404e6fbfa60690223e7 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, 20 Feb 2024 20:16:42 +0800 Subject: [PATCH] v1 --- server/server.go | 127 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 30 deletions(-) diff --git a/server/server.go b/server/server.go index fa90fd1..7cdaa51 100644 --- a/server/server.go +++ b/server/server.go @@ -5,16 +5,22 @@ import ( pkg2 "DifProject/pkg" "encoding/json" "fmt" + "io" "net/http" "sort" + "sync" "github.com/gorilla/websocket" ) var UP = websocket.Upgrader{ - WriteBufferSize: 0, - ReadBufferSize: 0, + ReadBufferSize: 1024, + WriteBufferSize: 1024, + // 允许跨域 + CheckOrigin: func(r *http.Request) bool { + return true + }, } var hub = make(map[string]map[string]*client2.Client) @@ -63,13 +69,9 @@ func getAuthentication(w http.ResponseWriter, r *http.Request) { } func SocketRoom(w http.ResponseWriter, r *http.Request) { - // 设置允许跨域的来源,*表示允许所有来源 - w.Header().Set("Access-Control-Allow-Origin", "*") - // 设置允许的请求方法 - w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") - // 设置允许的请求头 - w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") + conn, err := UP.Upgrade(w, r, nil) + if err != nil { fmt.Println(err) return @@ -79,14 +81,20 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { for { //读取socket请求 - msgType, msg, e := conn.ReadMessage() - if e != nil { - fmt.Println("readMessage fail") - return - } + //msgType, msg, e := conn.ReadMessage() + + //if e != nil { + // fmt.Println("readMessage fail") + // return + //} //解析数据包 var pkg pkg2.Pkg - json.Unmarshal(msg, &pkg) + //json.Unmarshal(msg, &pkg) + err := conn.ReadJSON(pkg) + if err != nil { + fmt.Println("") + return + } pkgType := pkg.Type if pkgType == pkg2.BuildRoom || pkgType == pkg2.JoinRoom { @@ -106,12 +114,11 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { Data: pkg.Data, } - marshal, err := json.Marshal(postPkg) + err := pkg.Data.Conn.WriteJSON(postPkg) if err != nil { + fmt.Println("build write error") return } - - pkg.Data.Conn.WriteMessage(msgType, marshal) fmt.Println("build success") //加入房间 @@ -143,17 +150,17 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { Data: pkg.Data, } - marshal1, _ := json.Marshal(postPkg1) - marshal2, _ := json.Marshal(postPkg2) + /*marshal1, _ := json.Marshal(postPkg1) + marshal2, _ := json.Marshal(postPkg2)*/ - err := conn1.WriteMessage(msgType, marshal2) + err := conn2.WriteJSON(postPkg1) if err != nil { - fmt.Println("房主写入失败") + fmt.Println("writeJSON1 fail") return } - err = conn2.WriteMessage(msgType, marshal1) + err = conn1.WriteJSON(postPkg2) if err != nil { - fmt.Println("被邀请者写入失败") + fmt.Println("writeJSON2 fail") return } @@ -170,16 +177,17 @@ func SocketRoom(w http.ResponseWriter, r *http.Request) { fmt.Println(111) } conn := hub[pkg.Data.RoomId][uid].Conn - marshal, err := json.Marshal(pkg) + + err := conn.WriteJSON(pkg) if err != nil { + fmt.Println("change error") return } - conn.WriteMessage(msgType, marshal) - case pkg2.HeartBeat: - //心跳检测 - if pkg.Data.Heart == "ping" { - fmt.Println(111) - } + //case pkg2.HeartBeat: + // //心跳检测 + // if pkg.Data.Heart == "ping" { + // fmt.Println(111) + // } } } } @@ -237,11 +245,70 @@ func postList(w http.ResponseWriter, r *http.Request) { } } +type Client struct { + Id uint64 `json:"id"` + Username string `json:"username"` + conn *websocket.Conn +} + +var ( + clients1 = make(map[uint64]*Client) + clientsLock sync.Mutex +) + func main() { http.HandleFunc("/login", getAuthentication) http.HandleFunc("/ws", SocketRoom) + //http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + // + // upgrader := websocket.Upgrader{ + // ReadBufferSize: 1024, + // WriteBufferSize: 1024, + // // 允许跨域 + // CheckOrigin: func(r *http.Request) bool { + // return true + // }, + // } + // + // conn, err := upgrader.Upgrade(w, r, nil) + // if err != nil { + // fmt.Println("websocket link failed: ", err) + // return + // } + // client := &Client{} + // err = conn.ReadJSON(client) + // if err != nil { + // //logc.Error(context.Background(), "websocket init failed: ", err) + // return + // } + // client.conn = conn + // clientsLock.Lock() + // clients1[client.Id] = client + // clientsLock.Unlock() + // conn.WriteJSON("success") + // defer func() { + // conn.Close() + // delete(clients1, client.Id) + // }() + // + // message := struct { + // Message string `json:"message"` + // To uint64 `json:"to"` + // }{} + // for { + // err := client.conn.ReadJSON(&message) + // if err != nil { + // //logc.Error(context.Background(), "client.conn.ReadJSON(&message) failed: ", err) + // continue + // } + // if c, has := clients1[message.To]; has { + // c.conn.WriteJSON(message) + // } + // } + // + //}) http.HandleFunc("/list", postList) http.ListenAndServe("0.0.0.0:8887", nil) -- Gitee