gofr-dev/gofr

View on GitHub
examples/using-web-socket/main.go

Summary

Maintainability
A
0 mins
Test Coverage
A
90%
package main

import (
    "gofr.dev/pkg/gofr"
)

func main() {
    app := gofr.New()

    app.WebSocket("/ws", WSHandler)

    app.Run()
}

func WSHandler(ctx *gofr.Context) (interface{}, error) {
    var message string

    err := ctx.Bind(&message)
    if err != nil {
        ctx.Logger.Errorf("Error binding message: %v", err)
        return nil, err
    }

    ctx.Logger.Infof("Received message: %s", message)

    err = ctx.WriteMessageToSocket("Hello! GoFr")
    if err != nil {
        return nil, err
    }

    return message, nil
}