lazyws

package module
v0.0.0-...-86cfdc9 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 5 Imported by: 0

README

LazyWS

A Wrapper To Simplify Web Socket

Examples
package main

import (
	"net/http"

	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/mohanavel15/lazyws"
)

func main() {
	upgrader := websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin:     func(r *http.Request) bool { return true },
	}

	lws := lazyws.NewLazyWS[any](upgrader)
	lws.On("PING", func(ctx *lazyws.Context[any]) {
		ctx.Send("PONG", []byte{})
	})

	router := mux.NewRouter()
	router.HandleFunc("/ws", lws.ServeHTTP)
	http.ListenAndServe(":5000", router)
}
Installation
go get github.com/mohanavel15/lazyws

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection[T any] struct {
	LWS     *LazyWS[T]
	Storage *T
	// contains filtered or unexported fields
}

func (*Connection[T]) Close

func (conn *Connection[T]) Close() error

func (*Connection[T]) Handler

func (conn *Connection[T]) Handler(data []byte)

func (*Connection[T]) Read

func (conn *Connection[T]) Read() ([]byte, error)

func (*Connection[T]) ReadLoop

func (conn *Connection[T]) ReadLoop()

func (*Connection[T]) Send

func (conn *Connection[T]) Send(data []byte) error

type Context

type Context[T any] struct {
	Event string
	Data  json.RawMessage
	Conn  *Connection[T]
}

func (*Context[T]) Send

func (ctx *Context[T]) Send(event string, data []byte) error

type EventHandler

type EventHandler[T any] struct {
	// contains filtered or unexported fields
}

func NewEventHandler

func NewEventHandler[T any]() EventHandler[T]

func (*EventHandler[T]) Dispatch

func (eh *EventHandler[T]) Dispatch(ctx Context[T])

func (*EventHandler[T]) On

func (eh *EventHandler[T]) On(event string, handler EventHandlerFunction[T])

type EventHandlerFunction

type EventHandlerFunction[T any] func(*Context[T])

type LazyWS

type LazyWS[T any] struct {
	EventHandler[T]

	Storage T
	// contains filtered or unexported fields
}

func NewLazyWS

func NewLazyWS[T any](upgrader websocket.Upgrader) LazyWS[T]

func (*LazyWS[T]) ServeHTTP

func (lws *LazyWS[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Message

type Message struct {
	Event string          `json:"event"`
	Data  json.RawMessage `json:"data"`
}

Jump to

Keyboard shortcuts

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