websocket

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConnUUIDCTXKey = "WebSocketConnUUIDCTXKey"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseTextMessageController

type BaseTextMessageController struct {
	Client       *Client
	Action       string
	ActionParams map[string]interface{}
}

func (*BaseTextMessageController) Init

func (*BaseTextMessageController) ParsePayload

func (b *BaseTextMessageController) ParsePayload(_ context.Context, client *Client, message ReceiveMessage) (err error)

type Client

type Client struct {
	UserId int64
	State  State // 客户端状态信息
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn *websocket.Conn, uuidStr string, userId int64) *Client

func (*Client) WriteTextMessage

func (c *Client) WriteTextMessage(ctx context.Context, message []byte) (err error)

type ClientManager

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

func NewClientManager

func NewClientManager() *ClientManager

func (*ClientManager) Delete

func (m *ClientManager) Delete(uuid string)

func (*ClientManager) IsExist

func (m *ClientManager) IsExist(uuid string) bool

func (*ClientManager) Len

func (m *ClientManager) Len() int

func (*ClientManager) Range

func (m *ClientManager) Range(f func(key, value any) bool)

func (*ClientManager) Store

func (m *ClientManager) Store(uuid string, c *Client)

type Config

type Config struct {
	HandshakeTimeoutSec time.Duration `json:"handshakeTimeoutSec"`
	ReadBufferSize      int           `json:"readBufferSize"`
	WriteBufferSize     int           `json:"writeBufferSize"`
	WriteBufferPool     int           `json:"writeBufferPool"`
	AllowedOrigins      []string      `json:"allowedOrigins"`
	EnableCompression   bool          `json:"enableCompression"`
}

type ReceiveMessage

type ReceiveMessage struct {
	SID          string                 `json:"sid"`
	Scene        string                 `json:"scene"`
	SceneParams  map[string]interface{} `json:"sceneParams"`
	Action       string                 `json:"action"`
	ActionParams map[string]interface{} `json:"actionParams"`
}
{
	"sid": "xxxxxxxxxxxxxx",
	"scene": "test",
	"sceneParams": {
		"key1": 1234,
		"key2": "value"
	},
	"action": "test",
	"actionParams": {
		"key1": 1234,
		"key2": "value"
	}
}

type RedisPubSub

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

func NewRedisPubSub

func NewRedisPubSub(channels ...string) *RedisPubSub

func (*RedisPubSub) Publish

func (r *RedisPubSub) Publish(ctx context.Context, redisClient *redis.Client, msg any) *redis.IntCmd

func (*RedisPubSub) Subscribe

func (r *RedisPubSub) Subscribe(ctx context.Context, redisClient *redis.Client, f func(context.Context, *redis.Message)) error

type State

type State struct {
	SID         string                 `json:"sid,omitempty"`
	Scene       string                 `json:"scene,omitempty"` // 场景信息
	SceneParams map[string]interface{} `json:"-"`
}

type TextMessageController

type TextMessageController interface {
	Init(base TextMessageController)
	ParsePayload(ctx context.Context, c *Client, message ReceiveMessage) error
	Process(ctx context.Context) error
}

type WebSocket

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

func New

func New(configPath string) *WebSocket

func (*WebSocket) GetClientManager

func (ws *WebSocket) GetClientManager() *ClientManager

func (*WebSocket) Handle

func (ws *WebSocket) Handle(ctx context.Context, path string, r *http.Request, w http.ResponseWriter)

func (*WebSocket) OnTextMessageController

func (ws *WebSocket) OnTextMessageController(scene string, controller TextMessageController)

func (*WebSocket) SetAuthorizationFunc

func (ws *WebSocket) SetAuthorizationFunc(f func(ctx context.Context, r *http.Request, w http.ResponseWriter) (returnCtx context.Context, isAuthorized bool, userId int64))

func (*WebSocket) SetBeforeFunc added in v0.1.7

func (ws *WebSocket) SetBeforeFunc(f func(ctx context.Context, r *http.Request, w http.ResponseWriter) (returnCtx context.Context, isAuthorized bool, userId int64))

func (*WebSocket) SetClientCloseFunc

func (ws *WebSocket) SetClientCloseFunc(f func(context.Context, string))

Jump to

Keyboard shortcuts

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