feat: add mutex lock to WebSocketClient for thread-safe message sending
This commit is contained in:
parent
ffc73605e8
commit
fa635ad9f1
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"git.lxtend.com/qqbot/action"
|
||||
"git.lxtend.com/qqbot/constants"
|
||||
@ -16,6 +17,7 @@ type WebSocketClient struct {
|
||||
conn *websocket.Conn
|
||||
closed bool
|
||||
Done chan struct{}
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewWebSocketClient(scheme, host, path string) (*WebSocketClient, error) {
|
||||
@ -88,6 +90,8 @@ func (c *WebSocketClient) receiveMessages() {
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) SendMessage(messageType int, message []byte) error {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.conn.WriteMessage(messageType, message)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user