websockets

package
v1.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package websockets is a wrapper for gofiber websocket.

Example:

  h.fiber.Get("/ws", websocket.New(func(c *websocket.Conn) {
	// get locals to use
	myLocal := c.Locals("myLocal")
	if myLocal == nil {
		log.Println("No myLocal")
		return
	}
	log.Println("Websocket opened")

	socketCtx, socketCancel := context.WithCancel(context.Background())
	sourceCtx, sourceCancel := context.WithCancel(context.Background())

	// use or implement custom buffer so non blocking
	// see videosource.RingBufferProcessedImage for an example which allows for drops
	// some services must guarantee msgs sent, use appropriate buffer type
	ringBuffer := customringbuffer.NewRingBuffer(1)
	stuffChan := h.myEngine.Subscribe("stuff")
	go func() {
		defer sourceCancel()
		// for select
		//   read stuffChan and push into ringBuffer
		//   unsubscribe from stuff if socketCtx.Done() or stale
	}()

	receive := func(msgType int, data []byte) {
		log.Println("Read Func called")
	}
	send := func(c *websocket.Conn) {
		// for select
		//   use ring buffer to write out to socket
		//   check sourceCtx.Done() and send all ring buffer then cleanup
	}
	cleanup := func() {
		// cleanup ring buffer
		log.Println("Websocket closed")
	}

	websockets.Run(socketCtx, socketCancel, c, receive, send, cleanup)
  }))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(socketCtx context.Context, socketCancel context.CancelFunc, c *websocket.Conn, receive func(int, []byte), send func(context.Context, *websocket.Conn), cleanup func())

Run is a wrapper for gofiber websocket

socketCtx - provides a way to gracefully shutdown goroutines when the websocket connection closes
socketCancel - cancels the socketCtx when errors occur or done
receive - do not block as it will continuously run until the websocket handler reaches the end
send - push out msgs and return when server is done with the websocket
cleanup - will be called when all goroutines are finished

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL