55 "errors"
66 "fmt"
77 "io"
8- "log"
98 "net"
109 "net/http"
1110 "time"
@@ -15,16 +14,19 @@ import (
1514 "github.com/google/uuid"
1615 "github.com/gorilla/websocket"
1716 "go.uber.org/fx"
17+ "go.uber.org/zap"
1818)
1919
2020var _ WebSocketRepository = (* WebSocket )(nil )
2121
2222type WebSocket struct {
2323 connections map [uuid.UUID ][]* websocket.Conn
24+ logger * zap.Logger
2425}
2526
2627type WebSocketParams struct {
2728 fx.In
29+ Logger * zap.Logger
2830}
2931
3032var upgrader = websocket.Upgrader {
@@ -39,6 +41,7 @@ func NewWebSocket(p WebSocketParams) WebSocketRepository {
3941 connections := make (map [uuid.UUID ][]* websocket.Conn )
4042 return & WebSocket {
4143 connections : connections ,
44+ logger : p .Logger ,
4245 }
4346}
4447
@@ -64,7 +67,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
6467 for {
6568 err := conn .SetReadDeadline (time .Now ().Add (30 * time .Second ))
6669 if err != nil {
67- fmt . Println ( err )
70+ ws . logger . Error ( "error setting read deadline" , zap . Error ( err ) )
6871 break
6972 }
7073
@@ -75,7 +78,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
7578 if message .Type == "ping" {
7679 errWrite := conn .WriteJSON (types.WebSocketMessage {Type : "pong" })
7780 if errWrite != nil {
78- fmt . Println ( errWrite )
81+ ws . logger . Error ( "error writing pong message" , zap . Error ( errWrite ) )
7982 }
8083 }
8184 continue
@@ -86,7 +89,7 @@ func (ws *WebSocket) AddConnection(ctx context.Context, w http.ResponseWriter, r
8689 break
8790 }
8891
89- fmt . Println ( err )
92+ ws . logger . Error ( "error reading json message" , zap . Error ( err ) )
9093 }
9194 conn .Close ()
9295 ws.connections [retrospectiveID ][i ] = nil
@@ -119,7 +122,7 @@ func (w *WebSocket) sendMessageToRetro(ctx context.Context, message types.WebSoc
119122 }
120123 err := conn .WriteJSON (message )
121124 if err != nil {
122- log . Printf ( "Error sending message %+v to connection: %v" , message , err )
125+ w . logger . Error ( "error sending message to connection" , zap . Any ( "message" , message ), zap . Error ( err ) )
123126 }
124127 }
125128
@@ -170,11 +173,11 @@ func (w *WebSocket) DeleteQuestion(ctx context.Context, id uuid.UUID) (*types.Qu
170173 return nil , w .sendMessageToRetro (ctx , message , nil )
171174}
172175
173- func (s * WebSocket ) GetOldRetrospectives (ctx context.Context , date time.Time ) ([]uuid.UUID , error ) {
176+ func (* WebSocket ) GetOldRetrospectives (ctx context.Context , date time.Time ) ([]uuid.UUID , error ) {
174177 panic ("unimplemented" )
175178}
176179
177- func (s * WebSocket ) GetAllRetrospectives (ctx context.Context ) ([]uuid.UUID , error ) {
180+ func (* WebSocket ) GetAllRetrospectives (ctx context.Context ) ([]uuid.UUID , error ) {
178181 panic ("unimplemented" )
179182}
180183
0 commit comments