-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85b145c
commit 20257d3
Showing
5 changed files
with
126 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package api | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/gofiber/contrib/websocket" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type ThreadSafeClient struct { | ||
Conn *websocket.Conn | ||
mu sync.Mutex | ||
} | ||
|
||
func NewThreadSafeClient(conn *websocket.Conn) *ThreadSafeClient { | ||
return &ThreadSafeClient{ | ||
Conn: conn, | ||
} | ||
} | ||
|
||
func (c *ThreadSafeClient) WriteJSON(data any) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if err := c.Conn.WriteJSON(data); err != nil { | ||
log.Error().Err(err).Msg("failed to write json msg") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *ThreadSafeClient) ReadJSON(data any) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if err := c.Conn.ReadJSON(&data); err != nil { | ||
log.Error().Err(err).Msg("failed to read json msg") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *ThreadSafeClient) Close() error { | ||
return c.Conn.Close() | ||
} | ||
|
||
func (c *ThreadSafeClient) WriteControl(messageType int, data []byte, deadline time.Time) error { | ||
return c.Conn.WriteControl(messageType, data, deadline) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters