Skip to content

Commit

Permalink
#14: Used JSON-based messages
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Feb 18, 2024
1 parent 746747f commit 23d8062
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/api/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (cfg *ServerConfig) ToServer() *fiber.App {
// More configs are listed on https://docs.gofiber.io/api/fiber
// TODO: Consider alternative JSON marshallers that provides better performance over the standard marshaller
serverConfig := fiber.Config{
AppName: "glide",
AppName: "Glide",
DisableDefaultDate: true,
ServerHeader: fmt.Sprintf("glide/%v", version.Version),
StreamRequestBody: true,
Expand Down
15 changes: 7 additions & 8 deletions pkg/api/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,25 @@ func LangStreamRouterValidator(routerManager *routers.RouterManager) Handler {
func LangStreamChatHandler() Handler {
// TODO: expose websocket connection configs https://github.com/gofiber/contrib/tree/main/websocket
return websocket.New(func(c *websocket.Conn) {
log.Println(c.Params("router"))
routerID := c.Params("router")
log.Println("routerID: ", routerID)

// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index

var (
mt int
msg []byte
err error
err error
chatRequest schemas.UnifiedChatRequest
)

for {
if mt, msg, err = c.ReadMessage(); err != nil {
if err = c.ReadJSON(&chatRequest); err != nil {
log.Println("read:", err)
break
}

log.Printf("msg type: %s", mt)
log.Printf("recv: %s", msg)
log.Printf("recv req: %s", chatRequest.Message.Content)

if err = c.WriteMessage(mt, msg); err != nil {
if err = c.WriteJSON(chatRequest); err != nil {
log.Println("write:", err)
break
}
Expand Down

0 comments on commit 23d8062

Please sign in to comment.