nezha/pkg/websocketx/safe_conn.go
2022-11-01 21:46:02 +08:00

26 lines
439 B
Go

package websocketx
import (
"sync"
"github.com/gorilla/websocket"
"github.com/samber/lo"
)
type Conn struct {
*websocket.Conn
writeLock sync.Mutex
}
func (conn *Conn) WriteMessage(msgType int, data []byte) error {
conn.writeLock.Lock()
defer conn.writeLock.Unlock()
var err error
lo.TryCatchWithErrorValue(func() error {
return conn.Conn.WriteMessage(msgType, data)
}, func(res any) {
err = res.(error)
})
return err
}