ws

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Websocket Wrapper

Write your Websocket message routing like http route handler.

Use the JSON RPC Specification

Usage

Get dependencies

go get github.com/darmawan01/ws-wrapper

Register websocket wrapper handler

engine := gin.Default()

router := engine.Group("/api")

/* Register websocket route */
router.GET("/ws", func(ctx *gin.Context) {
    /* Websocket wrapper handler */
    ws.WebsocketHandler(ctx.Writer, ctx.Request)
})
Registering your method handler
ws.RegisterChannelHandler("public/health", h.healthCheck)

Documentation

Index

Constants

View Source
const (
	// Parse error	Invalid JSON was received by the server An error occurred on the server while parsing the JSON text.
	InvalidJSON int64 = -32700
	// Invalid Request	The JSON sent is not a valid Request object.
	InvalidRequest int64 = -32600
	// Method not found	The method does not exist / is not available.
	MethodNotFound int64 = -32601
	// Invalid params	Invalid method parameter(s).
	Invalidparams int64 = -32602
	// Internal error	Internal JSON-RPC error.
	InternalError int64 = -32603
	// -32000 to -32099	Server error
	ServerError int64 = -32000
)
View Source
const (
	PING    = "PING"
	PONG    = "PONG"
	JSONRPC = "2.0"
)

Variables

This section is empty.

Functions

func GetChannels

func GetChannels() map[string]func(RequestMessage, *Client)

func RegisterChannelHandler

func RegisterChannelHandler(channel string, fn func(RequestMessage, *Client))

func SendMessageAndWaitForResponse

func SendMessageAndWaitForResponse(conn *websocket.Conn, req any, response chan ResponseMessage)

func WebsocketHandler

func WebsocketHandler(w http.ResponseWriter, r *http.Request)

Types

type Client

type Client struct {
	*websocket.Conn

	Context ClientContext
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn *websocket.Conn) *Client

func (*Client) OnClose

func (c *Client) OnClose(fn func())

func (*Client) Send

func (c *Client) Send(message ResponseMessage)

type ClientContext

type ClientContext struct {
	Context context.Context
}

func (*ClientContext) Get

func (c *ClientContext) Get(key ClientContextKey) any

func (*ClientContext) GetId

func (c *ClientContext) GetId() uint64

func (*ClientContext) Set

func (c *ClientContext) Set(key ClientContextKey, val any)

type ClientContextKey

type ClientContextKey string
const (
	ClientId ClientContextKey = "ClientId"
)

type ErrorMessage

type ErrorMessage struct {
	Message string         `json:"message"`
	Data    *ReasonMessage `json:"data"`
	Code    int64          `json:"code"`
}

type ReasonMessage

type ReasonMessage struct {
	Reason string `json:"reason"`
}

type RequestMessage

type RequestMessage struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	ID      *uint64     `json:"id,omitempty"`
	Params  interface{} `json:"params"`
}

type ResponseMessage

type ResponseMessage struct {
	JSONRPC string        `json:"jsonrpc"`
	ID      uint64        `json:"id,omitempty"`
	Method  string        `json:"method,omitempty"`
	Result  any           `json:"result,omitempty"`
	Params  any           `json:"params,omitempty"`
	UsIn    uint64        `json:"usIn,omitempty"`
	UsOut   uint64        `json:"usOut,omitempty"`
	UsDiff  uint64        `json:"usDiff,omitempty"`
	Error   *ErrorMessage `json:"error,omitempty"`
}

type WsHandlerFunc

type WsHandlerFunc func(RequestMessage, *Client)

func MiddlewaresWrapper

func MiddlewaresWrapper(handler WsHandlerFunc, middlewares ...WsMiddewareFunc) WsHandlerFunc

type WsMiddewareFunc

type WsMiddewareFunc func(RequestMessage, *Client) *ResponseMessage

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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