修复CPU占用问题,添加servers.JOIN,LEAVE,LIST包

This commit is contained in:
2025-07-11 02:27:16 +08:00
parent e5e02d2430
commit f5290d8585
13 changed files with 303 additions and 113 deletions

View File

@ -1,6 +1,9 @@
package ws
import "github.com/gorilla/websocket"
import (
"context"
"github.com/gorilla/websocket"
)
type Packet struct {
isBinary bool
@ -24,6 +27,7 @@ func Binary(buf []byte) *Packet {
type Conn struct {
WriteChan chan *Packet
ReadChan chan []byte
ctx context.Context
conn *websocket.Conn
close bool
}
@ -40,6 +44,7 @@ func (conn *Conn) ReadLoop() {
for !conn.close {
_, message, err := conn.conn.ReadMessage()
if err != nil {
conn.Close()
break
}
conn.ReadChan <- message
@ -67,6 +72,7 @@ func (conn *Conn) WriteLoop() {
}
func (conn *Conn) Close() error {
conn.ReadChan <- nil
conn.close = true
return conn.conn.Close()
}