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) {
defer socketCancel()
// 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, c, receive, send, cleanup)
}))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run(socketCtx context.Context, c *websocket.Conn, receive func(int, []byte), send func(*websocket.Conn), cleanup func())
Run is a wrapper for gofiber websocket
socketCtx - provides a way to gracefully shutdown goroutines when the websocket connection closes receive - do not block as it will continuously run until the websocket is closed 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.
Click to show internal directories.
Click to hide internal directories.