修改代码
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TCPHandler struct {
|
||||
@@ -18,17 +19,19 @@ type TCPHandler struct {
|
||||
func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
reader := bufio.NewReader(conn)
|
||||
clientID := conn.RemoteAddr().String()
|
||||
var client *Client
|
||||
if value, ok := h.Server.GetClient(clientID); ok {
|
||||
client = value
|
||||
} else {
|
||||
fmt.Println("找不到客户端:", clientID)
|
||||
return
|
||||
|
||||
// 创建临时客户端对象
|
||||
tempClient := &Client{
|
||||
ID: clientID,
|
||||
Conn: conn,
|
||||
ConnectedAt: time.Now(),
|
||||
LastPing: time.Now(),
|
||||
Done: make(chan struct{}),
|
||||
}
|
||||
|
||||
broadcastMessage := func(data []byte) {
|
||||
if h.Hub != nil {
|
||||
h.Hub.Broadcast <- &ws.WsMessage{IMEI: client.Imei, Data: string(data)}
|
||||
if h.Hub != nil && tempClient.IsAuth {
|
||||
h.Hub.Broadcast <- &ws.WsMessage{IMEI: tempClient.Imei, Data: string(data)}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,143 +69,146 @@ func (h *TCPHandler) HandleClient(conn net.Conn) {
|
||||
|
||||
switch msgType {
|
||||
case "reg":
|
||||
if err := h.Server.HandleAuth(client, message); err != nil {
|
||||
// 处理注册
|
||||
if err := h.Server.HandleAuth(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端授权失败: %v\n", err)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
fmt.Printf("客户端已授权: %s\n", client.Imei)
|
||||
// 注册成功后,将客户端添加到 clients map
|
||||
h.Server.addClient(tempClient)
|
||||
fmt.Printf("客户端已授权并注册: %s\n", tempClient.Imei)
|
||||
broadcastMessage(message)
|
||||
|
||||
case "ping":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的心跳 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.HandleHeartbeat(client, message); err != nil {
|
||||
if err := h.Server.HandleHeartbeat(tempClient, message); err != nil {
|
||||
fmt.Printf("心跳错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "ota":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.HandleOta(client, message); err != nil {
|
||||
if err := h.Server.HandleOta(tempClient, message); err != nil {
|
||||
fmt.Printf("OTA 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "start":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.RealTimeReporting(client, message); err != nil {
|
||||
if err := h.Server.RealTimeReporting(tempClient, message); err != nil {
|
||||
fmt.Printf("实时上报错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "stop":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.StopRealTimeReporting(client, message); err != nil {
|
||||
if err := h.Server.StopRealTimeReporting(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端停止实时上报数据 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "up":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.TimingReporting(client, message); err != nil {
|
||||
if err := h.Server.TimingReporting(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端定时上报数据 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
case "SetConfig":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.SetConfig(client, message); err != nil {
|
||||
if err := h.Server.SetConfig(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端楼层设置 错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "GetConfig":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.GetConfig(client, message); err != nil {
|
||||
if err := h.Server.GetConfig(tempClient, message); err != nil {
|
||||
fmt.Printf("获取客户端楼层设置错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
|
||||
case "mp3a", "mp3b", "mp3c":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.Mp3(client, message); err != nil {
|
||||
if err := h.Server.Mp3(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端设置语音内容错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
case "SetVoiceConf":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.SetVoiceConf(client, message); err != nil {
|
||||
if err := h.Server.SetVoiceConf(tempClient, message); err != nil {
|
||||
fmt.Printf("客户端语音配置错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
case "GetVoiceConf":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.GetVoiceConf(client, message); err != nil {
|
||||
if err := h.Server.GetVoiceConf(tempClient, message); err != nil {
|
||||
fmt.Printf("获取客户端语音配置错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
case "db":
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if err := h.Server.Db(client, message); err != nil {
|
||||
if err := h.Server.Db(tempClient, message); err != nil {
|
||||
fmt.Printf("获取客户端音量分贝错误: %v\n", err)
|
||||
continue
|
||||
}
|
||||
broadcastMessage(message)
|
||||
default:
|
||||
if !client.IsAuth {
|
||||
if !tempClient.IsAuth {
|
||||
fmt.Printf("来自未授权客户端的消息 %s\n", conn.RemoteAddr())
|
||||
conn.Close()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user