failed to get reader: use of closed network connection
srinivasan-getstan opened this issue · 3 comments
srinivasan-getstan commented
When ever first user tries to send message after the second user is joined. I'm getting error message failed to get reader: use of closed network connection
Find the code snippet below:
package websocket
import (
"club-service/config"
"club-service/internal/logger"
"club-service/internal/middleware"
"club-service/src/api/addClubUsers"
"club-service/src/api/clubCategories"
"club-service/src/api/createClub"
"club-service/src/api/deleteClub"
"club-service/src/api/editClub"
"club-service/src/api/getClubs"
firestore "club-service/src/fireStore"
"club-service/src/utils"
"club-service/src/wsMsgReader"
"fmt"
"net/http"
"github.com/google/uuid"
_websocket "nhooyr.io/websocket"
)
func InitiateSocketServer() {
http.HandleFunc("/ws", WsHandler)
logger.Log.Infof(`Initializing the Websocket server in PORT : %s`, config.EnvValues.SERVER_PORT)
if err := http.ListenAndServe(fmt.Sprintf(":%s", config.EnvValues.SERVER_PORT), nil); err != nil {
logger.Log.Fatalf(`failed to initialize the server.... %s`, err)
}
}
func heathCheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Healthy!")
}
func WsHandler(w http.ResponseWriter, r *http.Request) {
requestId, _ := uuid.NewUUID()
log := logger.Log.WithFields(map[string]interface{}{
"EVENT": "WEBSOCKET_HANDLER",
"REQUEST_ID": requestId,
})
wsConn, err := _websocket.Accept(w, r, nil)
if err != nil {
log.Errorf("Error accepting WebSocket connection: %v", err)
return
}
queryValues := r.URL.Query()
userId := utils.StringToInt32(queryValues.Get("userId"))
clubId := queryValues.Get("clubId")
log.Infof("Client connected to websocket : CLUB_ID : %s : USER_ID : %d", clubId, userId)
wsMsgReader.WsReader(r.Context(), wsConn)
logger.Log.Infof("Client disconnected : %d ", userId)
}
--------- WsReader -----------
package wsMsgReader
import (
"club-service/internal/logger"
"club-service/src/event"
"club-service/src/eventSchemaHandler"
"context"
flatbuffers "github.com/google/flatbuffers/go"
_websocket "nhooyr.io/websocket"
)
func WsReader(requestCtx context.Context, wsConn *_websocket.Conn) {
for {
// var msg string
// if err := wsjson.Read(requestCtx, wsConn, &msg); err != nil {
// fmt.Printf("failed to read message: %v\n", err)
// break
// }
// fmt.Printf("received message: %s\n", msg)
_, buff, err := wsConn.Read(requestCtx)
if err != nil {
logger.Log.Infof(`error ----> : %s`, err)
break
}
data := eventSchemaHandler.GetRootAsEventWrapper(buff, 0)
logger.Log.Infof("Received Event : %s\n", data.Event())
unionTable := new(flatbuffers.Table)
data.Payload(unionTable)
event.SerializeEvents(string(data.ClubId()), string(data.Event()), unionTable, wsConn)
}
}
srinivasan-getstan commented
Can someone help me
nhooyr commented
Most likely in your client code, nothing there stands out to me.
nhooyr commented
Going to close for now. Feel free to reopen if there's additional information.