websocket

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BroadcastNotification

func BroadcastNotification(notice *models.Notice)

func GenerateClientID

func GenerateClientID() string

func GetOnlineInfo

func GetOnlineInfo() gin.H

func HandleWebSocket

func HandleWebSocket(ctx *gin.Context)

func RegisterEventHandler

func RegisterEventHandler(event EventType, handler EventHandler)

func SendKickToUser

func SendKickToUser(userID string, reason string)

func SendNotificationToUser

func SendNotificationToUser(userID string, notice *models.Notice) bool

Types

type Client

type Client struct {
	ID     string
	UserID string
	Conn   *websocket.Conn
	Send   chan *WResponse

	HeartbeatTime time.Time
	IP            string
	UserAgent     string
	// contains filtered or unexported fields
}

func NewClient

func NewClient(id, userID string, conn *websocket.Conn, ip, userAgent string) *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) ReadPump

func (c *Client) ReadPump(onMessage func(*Client, *WRequest))

func (*Client) SendMsg

func (c *Client) SendMsg(msg *WResponse) bool

func (*Client) WritePump

func (c *Client) WritePump()

type EventHandler

type EventHandler func(*Client, json.RawMessage)

type EventType

type EventType string
const (
	EventPing   EventType = "ping"
	EventPong   EventType = "pong"
	EventNotify EventType = "notify"
	EventKick   EventType = "kick"
	EventJoin   EventType = "join"
	EventQuit   EventType = "quit"
)

type Hub

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

func GetHub

func GetHub() *Hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(msg *WResponse)

func (*Hub) GetOnlineCount

func (h *Hub) GetOnlineCount() int

func (*Hub) GetOnlineUserCount

func (h *Hub) GetOnlineUserCount() int

func (*Hub) IsUserOnline

func (h *Hub) IsUserOnline(userID string) bool

func (*Hub) Register

func (h *Hub) Register(client *Client)

func (*Hub) Run

func (h *Hub) Run()

func (*Hub) SendToUser

func (h *Hub) SendToUser(userID string, msg *WResponse)

func (*Hub) SendToUserDirect

func (h *Hub) SendToUserDirect(userID string, msg *WResponse) int

func (*Hub) SetOnMessage

func (h *Hub) SetOnMessage(fn func(*Client, *WRequest))

func (*Hub) Stop

func (h *Hub) Stop()

func (*Hub) Unregister

func (h *Hub) Unregister(client *Client)

type WRequest

type WRequest struct {
	Event EventType   `json:"event"`
	Data  interface{} `json:"data,omitempty"`
}

type WResponse

type WResponse struct {
	Event     EventType   `json:"event"`
	Data      interface{} `json:"data,omitempty"`
	Code      int         `json:"code"`
	ErrorMsg  string      `json:"errorMsg,omitempty"`
	Timestamp int64       `json:"timestamp"`
}

Jump to

Keyboard shortcuts

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