shizhongqi 11 miesięcy temu
rodzic
commit
3eccaefc25
2 zmienionych plików z 3 dodań i 10 usunięć
  1. 2 7
      main.go
  2. 1 3
      wsClient/room.go

+ 2 - 7
main.go

@@ -14,15 +14,10 @@ import (
 	"github.com/joho/godotenv"
 )
 
-
-
 func main() {
-	// makes every randomly generated number unique
-	//rand.Seed(time.Now().UnixNano()) //从 Go 1.20 开始,全局随机数生成器会自动使用一个随机种子,所以不再需要手动调用 Seed
-	
-	// 加载 .env 文件,如果文件不存在则忽略错误
+	// 加载 .env 文件
 	if err := godotenv.Load(); err != nil {
-		log.Println("没有找到 .env 文件,将使用系统环境变量和默认值")
+		panic("没有找到 .env 文件,将使用系统环境变量和默认值")
 	}
 	
 	var addr = flag.String("addr", ":8080", "Addr of the App")

+ 1 - 3
wsClient/room.go

@@ -32,7 +32,6 @@ type Room struct {
 	// forward is a channel that holds incoming messages that should be forwarded to the other clients.
 	// 用于接收需要转发给其他客户端的消息
 	// 所有向此房间发送的消息都会通过这个 channel 进行分发
-	// 现在包含消息内容和发送者信息,以便排除发送者自己
 	forward chan *message
 
 	// name 房间名称,用作Redis存储的键
@@ -40,10 +39,9 @@ type Room struct {
 }
 
 // message 包含要转发的消息内容和发送者信息
-// 用于在转发时排除发送者自己
 type message struct {
 	content []byte  // 消息内容
-	sender  *Client // 发送者
+	sender  *Client // 发送者 -- // 用处:1.用于在转发时排除发送者自己
 }
 
 // 全局互斥锁,用于保护 rooms 字典的并发访问,确保多个 goroutine 同时访问 rooms 时的线程安全