Go - Websocket.Close()

Closes a connection to a websocket

import (
  "context"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  ws := nitric.NewWebsocket("public")

  ws.Close(context.TODO(), "D28BA458-BFF4-404A")

  nitric.Run()
}

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 (
  "context"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/websockets"
)

func main() {
  ws := nitric.NewWebsocket("public")

  // Broadcast message to all the registered websocket connections
  ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) error {
    if ctx.Request.Message() == "close" {
      err := ws.Close(context.Background(), ctx.Request.ConnectionID())
      return err
    }

    return nil
  })

  nitric.Run()
}