ws

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authentication

type Authentication struct {
}

func (*Authentication) Authenticate

func (p *Authentication) Authenticate(r *http.Request, c *gin.Context, conn *websocket.Conn) (int, bool)

Authenticate todo: implement Authenticate method, based on jwt token auth 1. get token key from http header 2. if not get token key from cookies 3. if not get token key from first message 4. parse token key to find user

type AuthenticationI

type AuthenticationI interface {
	Authenticate(r *http.Request, c *gin.Context, conn *websocket.Conn) (int, bool)
}

AuthenticationI authenticate conn users

type CodecI

type CodecI interface {
	Encode() ([]byte, error)
	Decode([]byte) error
}

CodecI encode decode conn message

type Context

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

Context websocket context

func NewContext

func NewContext(options ...CtxOption) *Context

NewContext 实例化 websocket context function

func (*Context) Close

func (p *Context) Close() error

func (*Context) GetConn

func (p *Context) GetConn() *websocket.Conn

GetConn 获取 底层 websocket.Conn 实例

func (*Context) GetHub

func (p *Context) GetHub() HubI

func (*Context) GetId

func (p *Context) GetId() int

GetId 获取 conn 对应的 id

func (*Context) Go

func (p *Context) Go()

func (*Context) HandShake

func (p *Context) HandShake(r *http.Request, c *gin.Context) bool

HandShake 握手回调

func (*Context) Message

func (p *Context) Message(b []byte)

func (*Context) Open

func (p *Context) Open(r *http.Request, c *gin.Context) bool

Open Open回调

func (*Context) Send

func (p *Context) Send(message []byte)

func (*Context) SetUp

func (p *Context) SetUp(id int, conn *websocket.Conn, hub HubI)

SetUp hub 建立连接初始化 context 操作

type ContextConnI

type ContextConnI interface {
	SetUp(id int, conn *websocket.Conn, hub HubI)
	Send(message []byte)

	GetHub() HubI
	GetId() int
	GetConn() *websocket.Conn

	Go()

	HandShake(r *http.Request, c *gin.Context) bool
	Open(r *http.Request, c *gin.Context) bool
	Close() error
}

ContextConnI context conn interface

type CtxOption

type CtxOption func(c *Context)

CtxOption functional options

func Codec

func Codec(codec CodecI) CtxOption

Codec 配置编码器

func Handler

func Handler(handler HandlerI) CtxOption

Handler 配置业务处理器

type DefaultHandler

type DefaultHandler struct {
}

DefaultHandler 默认的示例 处理业务接口

func (*DefaultHandler) Close

func (p *DefaultHandler) Close(ctx ContextConnI)

Close 关闭连接操作

func (*DefaultHandler) HandShake

func (p *DefaultHandler) HandShake(r *http.Request, c *gin.Context) bool

HandShake 检查 origin 是否正确 在连接正式建立之前

func (*DefaultHandler) Message

func (p *DefaultHandler) Message(ctx ContextConnI, msg []byte)

Message read callback 处理收到的消息信令

func (*DefaultHandler) Open

func (p *DefaultHandler) Open(r *http.Request, c *gin.Context) bool

Open open 连接已经建立

type HandlerI

type HandlerI interface {
	HandShake(r *http.Request, c *gin.Context) bool
	Open(r *http.Request, c *gin.Context) bool
	Message(ctx ContextConnI, msg []byte)
	Close(ctx ContextConnI)
}

HandlerI conn handler interface

type Hub

type Hub struct {
	// Inbound messages from the clients.
	Broadcast chan []byte
	// contains filtered or unexported fields
}

func NewHub

func NewHub(options ...HubOption) *Hub

func (*Hub) FindContextConnById

func (h *Hub) FindContextConnById(id int) (ctx ContextConnI)

func (*Hub) GetLogger

func (h *Hub) GetLogger() log.Logger

func (*Hub) IdIsOnLine

func (h *Hub) IdIsOnLine(id int) (onLine bool, ctx ContextConnI)

func (*Hub) OnlineMapping

func (h *Hub) OnlineMapping() map[int]ContextConnI

func (*Hub) Register

func (h *Hub) Register(id int, i ContextConnI)

func (*Hub) Serve

func (h *Hub) Serve(w http.ResponseWriter, r *http.Request, c *gin.Context, authentication AuthenticationI, ctx ContextConnI)

Serve 1. context 2. id 3. conn 4. open message close 5. go writer reader

func (*Hub) TotalOnline

func (h *Hub) TotalOnline() int

func (*Hub) UnRegister

func (h *Hub) UnRegister(id int)

type HubI

type HubI interface {
	TotalOnline() int

	UnRegister(id int)
	Register(id int, i ContextConnI)

	FindContextConnById(id int) ContextConnI
	IdIsOnLine(id int) (bool, ContextConnI)

	GetLogger() log.Logger
}

HubI hub ws interface websocket 长链接相关接口方法的定义

type HubOption

type HubOption func(c *Hub)

HubOption functional options

func Logger

func Logger(logger log.Logger) HubOption

Logger 配置业务处理器

Jump to

Keyboard shortcuts

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