websocket

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Max = 1000
)

Variables

This section is empty.

Functions

func EventListener

func EventListener(interval time.Duration, eventFunc func(), stopChan <-chan struct{})

func NewClientManager

func NewClientManager()

func Upgrade

func Upgrade(w http.ResponseWriter, req *http.Request)

Types

type Client

type Client struct {
	Fd           string          // 每个连接唯一标识
	Request      *http.Request   // 请求
	Socket       *websocket.Conn // 用户连接
	Send         chan []byte     // 待发送的数据
	Channel      sync.Map        // 订阅频道
	OwnerChannel sync.Map        // 自己创建的频道

	FirstTime     int64 // 首次连接事件
	HeartbeatTime int64 // 用户上次心跳时间
	Timeout       int64 // 超时断连时间
	Protocol      int   // 协议类型
	// contains filtered or unexported fields
}

func NewClient

func NewClient(socket *websocket.Conn, req *http.Request) *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) CloseSubscribe

func (c *Client) CloseSubscribe(channel string) (err error)

func (*Client) CreatChan

func (c *Client) CreatChan() (channel string, err error)

CreatChan Creat channel

func (*Client) GetAllChan

func (c *Client) GetAllChan() (channels []string, pubSubs []*redis.PubSub)

GetAllChan Get all channels

func (*Client) GetChan

func (c *Client) GetChan(channel string) (pubSub *redis.PubSub, err error)

GetChan Get channel

func (*Client) Heartbeat

func (c *Client) Heartbeat()

Heartbeat The scheduled task clears timeout links

func (*Client) IsHeartbeatTimeout

func (c *Client) IsHeartbeatTimeout(currentTime int64) (timeout bool)

func (*Client) Publish

func (c *Client) Publish(channel string, message []byte) (err error)

Publish a message

func (*Client) Read

func (c *Client) Read()

Read client data

func (*Client) Receive

func (c *Client) Receive()

Receive subscription messages

func (*Client) SendMessage

func (c *Client) SendMessage(message []byte)

SendMessage Message distribution

func (*Client) SetHeartbeatTime

func (c *Client) SetHeartbeatTime(currentTime int64)

func (*Client) Subscribe

func (c *Client) Subscribe(channel string) (err error)

Subscribe to messages for long links

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(channel string) (err error)

Unsubscribe unsubscribe

func (*Client) Write

func (c *Client) Write()

Write Send data to the client

type ClientManager

type ClientManager struct {
	Pool      sync.Map
	Register  chan *Client
	Unset     chan *Client
	Total     uint
	Max       uint
	Broadcast chan []byte
	Errs      chan error
}

ClientManager Client pool manager

var (
	Manager *ClientManager
)

func (*ClientManager) Close

func (m *ClientManager) Close(client *Client)

Close Unset client

func (*ClientManager) GetAllClient

func (m *ClientManager) GetAllClient() (clients []*Client)

GetAllClient Get all client

func (*ClientManager) GetClient

func (m *ClientManager) GetClient(fd string) (client *Client, err error)

GetClient Obtain the client according to fd

func (*ClientManager) RegisterClient

func (m *ClientManager) RegisterClient(client *Client)

RegisterClient Register client

func (*ClientManager) Scheduler

func (m *ClientManager) Scheduler()

Scheduler Start the websocket scheduler

func (*ClientManager) UnsetClient added in v1.1.3

func (m *ClientManager) UnsetClient(fd string) (err error)

UnsetClient Unset client

type DisposeFunc

type DisposeFunc func(*Request) *Response

type DistributeHandler

type DistributeHandler interface {
	Distribute(client *Client, message []byte) (err error)
}

type JsonHandler

type JsonHandler struct{}

func NewJsonHandler

func NewJsonHandler() *JsonHandler

func (*JsonHandler) Distribute

func (j *JsonHandler) Distribute(client *Client, message []byte) (err error)

func (*JsonHandler) Do

func (j *JsonHandler) Do(client *Client, response *Response) (err error)

type Message

type Message struct {
	Event   string `json:"event" validate:"required"`
	Request string `json:"request"`
}

type MiddlewareFunc

type MiddlewareFunc func(DisposeFunc) DisposeFunc

type ProtoHandler

type ProtoHandler struct{}

func NewProtoHandler

func NewProtoHandler() *ProtoHandler

func (*ProtoHandler) Distribute

func (p *ProtoHandler) Distribute(client *Client, message []byte) (err error)

func (*ProtoHandler) Do

func (p *ProtoHandler) Do(client *Client, response *pb.Response) (err error)

type Request

type Request struct {
	Client *Client
	Send   []byte
}

type Response

type Response struct {
	Code    uint32 `json:"code"`
	Message string `json:"message"`
	Content string `json:"content"`
}

func NewResponse

func NewResponse() *Response

type Route

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

func (*Route) Execute

func (r *Route) Execute(request *Request) *Response

Execute the middleware chain

func (*Route) Use

func (r *Route) Use(middleware MiddlewareFunc)

Use Route Create a new middleware chain

type Router

type Router struct {
	// contains filtered or unexported fields
}
var (
	RouteManager *Router
)

func NewRouter

func NewRouter() *Router

func (*Router) GetRoute

func (r *Router) GetRoute(event string) (route *Route, err error)

GetRoute Get register's route

func (*Router) Register

func (r *Router) Register(event string, handler DisposeFunc, middlewareFunc ...MiddlewareFunc)

Register event to memory

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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