Go is in development for v1. Interested in contributing or chatting with us?Get in touch!
Go - Websocket.Close()
Closes a connection to a websocket
import (
"context"
"fmt"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
ws, err := nitric.NewWebsocket("public")
if err != nil {
return
}
ws.Close(context.TODO(), "D28BA458-BFF4-404A")
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
ctx
- Required
- Required
- Type
- context
- Description
The context of the call, used for tracing.
- Name
connectionId
- Required
- Required
- Type
- string
- Description
The ID of the connection which should be closed.
Examples
Close a connection to the websocket on message
import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
ws, err := nitric.NewWebsocket("public")
if err != nil {
return
}
// Broadcast message to all the registered websocket connections
ws.On(faas.WebsocketMessage, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
message := string(ctx.Request.Data())
if message == "close" {
err := ws.Close(ctx.Request.Context(), ctx.Request.ConnectionID())
return ctx, err
}
return next(ctx)
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}