websocket

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2025 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSignatureConfig = errors.New("bad config for Signature")

ErrSignatureConfig is an error that indicates bad config for signature.

Functions

func AddConnPool

func AddConnPool(userID int64, conn *connection)

func NewConn

func NewConn(ctx context.Context, userID int64, server *Server, r *http.Request, wsConn *websocket.Conn) *connection

创建ws连接

func RegisterSubscribeCheck

func RegisterSubscribeCheck(f func(ctx context.Context, in *SubscribeInfo) error)

func RegisterSubscribeCheck2

func RegisterSubscribeCheck2(f func(ctx context.Context, in *SubscribeInfo) ([]map[string]any, error))

func SendSub

func SendSub(ctx context.Context, body WsResp)

发送订阅信息

func StartWsDp

func StartWsDp(s2cGzip bool, NodeID int64, event *eventBus.FastEvent, c cache.ClusterConf)

创建ws调度器

func WithMiddleware

func WithMiddleware(middleware rest.Middleware, rs ...rest.Route) []rest.Route

WithMiddleware adds given middleware to given route.

func WithMiddlewares

func WithMiddlewares(ms []rest.Middleware, rs ...rest.Route) []rest.Route

WithMiddlewares adds given middlewares to given routes.

Types

type Ping

type Ping struct {
	Ping int64 `json:"ping"` //时间戳
}

type Pong

type Pong struct {
	Pong int64 `json:"pong"` //时间戳
}

type RouteOption

type RouteOption func(r *featuredRoutes)

func WithPrefix

func WithPrefix(group string) RouteOption

WithPrefix adds group as a prefix to the route paths.

func WithPriority

func WithPriority() RouteOption

WithPriority returns a RunOption with priority.

type RunOption

type RunOption func(*Server)

RunOption defines the method to customize a Server.

func WithNotAllowedHandler

func WithNotAllowedHandler(handler http.Handler) RunOption

WithNotAllowedHandler returns a RunOption with not allowed handler set to given handler.

func WithNotFoundHandler

func WithNotFoundHandler(handler http.Handler) RunOption

WithNotFoundHandler returns a RunOption with not found handler set to given handler.

func WithRouter

func WithRouter(router httpx.Router) RunOption

WithRouter returns a RunOption that make server run with given router.

type Server

type Server struct {
	// contains filtered or unexported fields
}

A Server is a http server.

func MustNewServer

func MustNewServer(c rest.RestConf, opts ...RunOption) *Server

MustNewServer returns a server with given config of c and options defined in opts. Be aware that later RunOption might overwrite previous one that write the same option. The process will exit if error occurs.

func NewServer

func NewServer(c rest.RestConf, opts ...RunOption) (*Server, error)

NewServer returns a server with given config of c and options defined in opts. Be aware that later RunOption might overwrite previous one that write the same option.

func (*Server) AddRoute

func (s *Server) AddRoute(r rest.Route, opts ...RouteOption)

AddRoute adds given route into the Server.

func (*Server) AddRoutes

func (s *Server) AddRoutes(rs []rest.Route, opts ...RouteOption)

AddRoutes add given routes into the Server.

func (*Server) PrintRoutes

func (s *Server) PrintRoutes()

PrintRoutes prints the added routes to stdout.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type SubscribeInfo

type SubscribeInfo struct {
	Code   string         `json:"code"`
	Params map[string]any `json:"params"`
}

type UserSubscribe

type UserSubscribe struct {
	ServerMsg *eventBus.FastEvent
	// contains filtered or unexported fields
}

func NewUserSubscribe

func NewUserSubscribe(store kv.Store, ServerMsg *eventBus.FastEvent) *UserSubscribe

func (*UserSubscribe) Publish

func (u *UserSubscribe) Publish(ctx context.Context, code string, data any, params ...map[string]any) error

type WsBody

type WsBody struct {
	Handler map[string]string `json:"handler,omitempty"`
	Type    WsType            `json:"type,omitempty"` //req 请求类型
	Path    string            `json:"path,omitempty"` //url路径或发布及订阅的主题
	Body    any               `json:"body,omitempty"` //消息体
}

type WsPublish

type WsPublish struct {
	Code   string
	Data   any
	Params []string
}

type WsPublishes

type WsPublishes []WsPublish

type WsReq

type WsReq struct {
	// Method specifies the HTTP method (GET, POST, PUT, etc.).
	// For client requests, an empty string means GET.
	//
	// Go's HTTP client does not support sending a request with
	// the CONNECT method. See the documentation on Transport for
	// details.
	Method string `json:"method"`
	WsBody
}

type WsResp

type WsResp struct {
	Code int64  `json:"code,omitempty"`
	Msg  string `json:"msg,omitempty"`
	WsBody
}

type WsType

type WsType string
const (
	Sub        WsType = "up.sub"          //订阅
	SubRet     WsType = "down.subRet"     //订阅回复
	Pub        WsType = "down.pub"        //发布
	Control    WsType = "up.control"      //控制
	ControlRet WsType = "down.controlRet" //控制回复
	UnSub      WsType = "up.unSub"        //取消订阅
	UnSubRet   WsType = "down.unSubRet"   //取消订阅回复
	SetHandle  WsType = "up.setHandle"    //设置http头
	UpPing     WsType = "up.ping"         //测试ws连接,发啥返回啥
	DownPong   WsType = "down.pong"       //测试ws连接,发啥返回啥

)

Jump to

Keyboard shortcuts

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