Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to get reader: use of closed network connection #439

Closed
srinivasan-getstan opened this issue Mar 1, 2024 · 3 comments
Closed

failed to get reader: use of closed network connection #439

srinivasan-getstan opened this issue Mar 1, 2024 · 3 comments

Comments

@srinivasan-getstan
Copy link

srinivasan-getstan commented Mar 1, 2024

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
Copy link
Author

Can someone help me

@nhooyr
Copy link
Owner

nhooyr commented Mar 1, 2024

Most likely in your client code, nothing there stands out to me.

@nhooyr
Copy link
Owner

nhooyr commented Mar 7, 2024

Going to close for now. Feel free to reopen if there's additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants