|
|
@@ -295,10 +295,10 @@ func (r *Room) HttpServe(w http.ResponseWriter, req *http.Request,roomParams *Ro
|
|
|
// ----------------------------------- 生成客户端对象 ----------------------------------
|
|
|
// 创建新的客户端对象,使用解析出的参数
|
|
|
client := &Client{
|
|
|
+ name: clientName(roomParams),
|
|
|
socket: socket, // WebSocket 连接
|
|
|
receive: make(chan []byte, messageBufferSize), // 接收消息的缓冲 channel
|
|
|
room: r, // 使用当前房间实例
|
|
|
- name: roomParams.Username, // 使用传递的用户名
|
|
|
userInfo: UserInfo{
|
|
|
UserId: roomParams.UserId,
|
|
|
Username: roomParams.Username,
|
|
|
@@ -322,67 +322,4 @@ func (r *Room) HttpServe(w http.ResponseWriter, req *http.Request,roomParams *Ro
|
|
|
go client.write()
|
|
|
// 在当前 goroutine 中处理客户端的读消息(阻塞直到连接断开)
|
|
|
client.read()
|
|
|
-}
|
|
|
-
|
|
|
-// ServeHTTP 处理 WebSocket 连接请求
|
|
|
-// 实现了 http.Handler 接口,负责将 HTTP 连接升级为 WebSocket 连接,并创建新的客户端加入当前房间
|
|
|
-func (r *Room) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
- // 解析WebSocket子协议中的参数
|
|
|
- protocols := req.Header["Sec-Websocket-Protocol"]
|
|
|
- roomParams, err := ParseWebSocketProtocols(protocols)
|
|
|
- if err != nil {
|
|
|
- log.Printf("解析WebSocket协议参数失败: %v", err)
|
|
|
- http.Error(w, "Invalid WebSocket protocols", http.StatusBadRequest)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // 验证房间名称是否匹配
|
|
|
- if roomParams.RoomName != r.name {
|
|
|
- log.Printf("房间名称不匹配: 期望 %s, 收到 %s", r.name, roomParams.RoomName)
|
|
|
- http.Error(w, "Room name mismatch", http.StatusBadRequest)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // 将 HTTP 连接升级为 WebSocket 连接,指定接受的子协议
|
|
|
- responseHeader := http.Header{}
|
|
|
- responseHeader.Add("Sec-WebSocket-Protocol", "chat") // 响应基础聊天协议
|
|
|
-
|
|
|
- socket, err := upgrader.Upgrade(w, req, responseHeader)
|
|
|
- if err != nil {
|
|
|
- log.Println("Upgrade error:", err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- // ----------------------------------- 生成客户端对象 ----------------------------------
|
|
|
- // 创建新的客户端对象,使用解析出的参数
|
|
|
- client := &Client{
|
|
|
- socket: socket, // WebSocket 连接
|
|
|
- receive: make(chan []byte, messageBufferSize), // 接收消息的缓冲 channel
|
|
|
- room: r, // 使用当前房间实例
|
|
|
- name: roomParams.Username, // 使用传递的用户名
|
|
|
- userInfo: UserInfo{
|
|
|
- UserId: roomParams.UserId,
|
|
|
- Username: roomParams.Username,
|
|
|
- CustomId: roomParams.CustomId,
|
|
|
- ShopId: roomParams.ShopId,
|
|
|
- Platform: roomParams.Platform,
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- // 如果用户名为空,生成一个默认用户名
|
|
|
- if client.name == "" {
|
|
|
- client.name = fmt.Sprintf("user_%d", rand.Intn(1000))
|
|
|
- }
|
|
|
-
|
|
|
- log.Printf("新客户端连接: 用户名=%s, 用户ID=%d, 自定义ID=%d, 店铺ID=%d, 平台=%s, 客户端类型=%s\n\n",
|
|
|
- client.userInfo.Username, client.userInfo.UserId, client.userInfo.CustomId, client.userInfo.ShopId, client.userInfo.Platform, client.userInfo.Type)
|
|
|
-
|
|
|
- // 将客户端加入当前房间
|
|
|
- r.join <- client
|
|
|
- defer func() { r.leave <- client }()
|
|
|
-
|
|
|
- // 启动客户端的写消息 goroutine
|
|
|
- go client.write()
|
|
|
- // 在当前 goroutine 中处理客户端的读消息(阻塞直到连接断开)
|
|
|
- client.read()
|
|
|
-}
|
|
|
+}
|