websocket

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package websocket implements a simple websocket client.

Package websocket implements a simple websocket connection manager.

Index

Constants

View Source
const (
	TypeSys       = "sys"
	TypeHeartbeat = "heartbeat"
)

Variables

This section is empty.

Functions

func ProcessMsg

func ProcessMsg(cli *Client, message []byte)

Types

type Callback

type Callback func(client *Client, message []byte)

Callback is the callback function for processing messages.

type Client

type Client struct {
	Addr         string // client address
	User         string
	Socket       *websocket.Conn // user connection
	IsActive     bool
	Send         chan []byte // data to send
	AccessTime   int64       // access timestamp
	OnDisconnect func(*Client)
}

Client is simple ws cli

func NewClient

func NewClient(addr string, user string, socket *websocket.Conn) (client *Client)

NewClient initializes a new client.

func (*Client) Read

func (c *Client) Read(processData Callback)

Read reads messages from the client.

func (*Client) SendMsg

func (c *Client) SendMsg(msg []byte)

SendMsg sends a message to the client.

func (*Client) Write

func (c *Client) Write()

Write writes messages to the client.

type ConnManagerIface

type ConnManagerIface interface {
	Register(cli *Client)
	Unregister(cli *Client)
	Heartbeat(cli *Client)
	DestroyTask()
	BroadcastSession(sessionID string, msg []byte)
}

ConnManagerIface is the interface for the connection manager.

func NewWsConnManage

func NewWsConnManage() ConnManagerIface

type RecvMsg

type RecvMsg struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
}

type SendMsg

type SendMsg struct {
	Status  int8   `json:"status"`
	MsgFrom string `json:"msg_from"`
	Msg     string `json:"msg"`
}

func (*SendMsg) DoSend

func (s *SendMsg) DoSend(cli *Client)

type Server

type Server struct {
	Register    chan *Client     // register connection handler
	Unregister  chan *Client     // unregister connection handler
	Heartbeat   chan *Client     // heartbeat handler
	ConnManager ConnManagerIface // connection manager
}

Server ws cli manage

var WsServer *Server

func NewServer

func NewServer(connManage ConnManagerIface) (clientManager *Server)

NewServer initializes a new server.

func (*Server) Start

func (c *Server) Start()

Start starts the server.

type WsConnManage

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

func (*WsConnManage) BroadcastSession

func (w *WsConnManage) BroadcastSession(sessionID string, msg []byte)

func (*WsConnManage) ClientsOfSession

func (w *WsConnManage) ClientsOfSession(sessionID string) []*Client

func (*WsConnManage) DestroyTask

func (w *WsConnManage) DestroyTask()

func (*WsConnManage) Heartbeat

func (w *WsConnManage) Heartbeat(cli *Client)

func (*WsConnManage) Register

func (w *WsConnManage) Register(cli *Client)

func (*WsConnManage) Unregister

func (w *WsConnManage) Unregister(cli *Client)

Jump to

Keyboard shortcuts

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