guesstimate/server/internal/route/project.go

110 lines
2.2 KiB
Go
Raw Normal View History

package route
import (
"log"
"net/http"
"github.com/davecgh/go-spew/spew"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
var upgrader = websocket.Upgrader{} // nolint: gochecknoglobals
func handleProjectWebsocket(w http.ResponseWriter, r *http.Request) {
log.Println("websocket request")
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
panic(errors.Wrap(err, "could not upgrade connection"))
}
defer c.Close()
// ctn := container.Must(r.Context())
// ctx := r.Context()
// Loop over incoming websocket messages
for {
_, data, err := c.ReadMessage()
if err != nil {
cause := errors.Cause(err)
if websocket.IsCloseError(cause, 1001, 1005) { // Ignore "going away" and "no status" close errors
return
}
logger.Error(
r.Context(),
"could not read message",
logger.E(err),
)
break
}
spew.Dump(data)
// message := &game.Message{}
// if err := json.Unmarshal(data, message); err != nil {
// logger.Error(
// r.Context(),
// "could not decode message",
// logger.E(err),
// )
// break
// }
// switch {
// case message.Type == game.MessageTypeInit:
// payload := &game.InitPayload{}
// if err := json.Unmarshal(message.Payload, payload); err != nil {
// logger.Error(
// r.Context(),
// "could not decode payload",
// logger.E(err),
// )
// break
// }
// setGameID(payload.GameID)
// evt := game.NewEventPlayerConnected(payload.GameID, user.ID)
// bus.Publish(game.EventNamespace, evt)
// case message.Type == game.MessageTypeGameEvent:
// gameID, ok := getGameID()
// if !ok {
// logger.Error(
// r.Context(),
// "game id not received yet",
// )
// break
// }
// payload := &game.EventPayload{}
// if err := json.Unmarshal(message.Payload, payload); err != nil {
// logger.Error(
// r.Context(),
// "could not decode payload",
// logger.E(err),
// )
// break
// }
// evt := game.NewEventPlayerMessage(gameID, user.ID, payload.Data)
// bus.Publish(game.EventNamespace, evt)
// default:
// logger.Error(
// r.Context(),
// "unsupported message type",
// logger.F("messageType", message.Type),
// )
// }
}
}