ws

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MSG_CLIENT_CONNECT          = "Client connected %s to Hub:%s"
	MSG_CLIENT_DISCONNECT       = "Client disconnected %s to Hub:%s"
	ERR_CLIENT_NOT_FOUND        = "Client not found"
	ERR_CLIENT_IS_CLOSED        = "Client is closed"
	ERR_NOT_WS_SERVICE          = "Not websocket service"
	ERR_NOT_DEFINE_CLIENTID     = "Not define clientId"
	ERR_NOT_DEFINE_USERNAME     = "Not define username"
	ERR_NOT_CONNECT_WS          = "Not connect socket"
	ERR_CHANNEL_NOT_FOUND       = "Channel not found"
	ERR_CHANNEL_NOT_SUBSCRIBERS = "Channel not subscribers - %s"
	ERR_CHANNEL_EMPTY           = "Channel is empty"
	ERR_PARAM_NOT_FOUND         = "Param not found"
	ERR_CLIENT_ID_EMPTY         = "Client id is empty"
	ERR_MESSAGE_UNFORMATTED     = "Message unformatted"
	ERR_REDISADAPTER_NOT_FOUND  = "Redis adapter not found"
	PARAMS_UPDATED              = "Params updated"
)

Variables

This section is empty.

Functions

func Close added in v0.0.2

func Close() error

Close the Websocket Hub

func ConnectWs added in v0.0.2

func ConnectWs(host, scheme, clientId, name string) (*websocket.Conn, error)

Connect to the ws server from the client

Types

type Channel

type Channel struct {
	Name        string
	TpBroadcast TpBroadcast
	Subscribers []*Client
	// contains filtered or unexported fields
}

Channel is a hub websocket channel

func (*Channel) Count

func (ch *Channel) Count() int

Count return the number of subscribers in channel

func (*Channel) Low added in v0.0.2

func (ch *Channel) Low() string

Low return the channel name in lowercase

func (*Channel) NextTurn added in v0.0.2

func (ch *Channel) NextTurn() *Client

func (*Channel) Subscribe added in v0.0.2

func (ch *Channel) Subscribe(client *Client)

Subscribe a client to channel

func (*Channel) Unsubcribe added in v0.0.2

func (ch *Channel) Unsubcribe(clientId string) error

Unsubcribe a client from channel

func (*Channel) Up added in v0.0.2

func (ch *Channel) Up() string

Up return the channel name in uppercase

type Client

type Client struct {
	Created_at time.Time

	Id   string
	Name string
	Addr string

	Channels []string
	// contains filtered or unexported fields
}

func ConnectHttp added in v0.0.2

func ConnectHttp(w http.ResponseWriter, r *http.Request) (*Client, error)

Connect to the server from the http client

func (*Client) Identify added in v0.0.2

func (c *Client) Identify() et.Json

Identify the client

type Conn added in v0.0.2

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

func Server added in v0.0.2

func Server() (*Conn, error)

Load the Websocket Hub

type Hub

type Hub struct {
	Id     string
	Name   string
	Params *HubParams
	// contains filtered or unexported fields
}

func NewHub

func NewHub() *Hub

Create a new hub

func (*Hub) GetSubscribers added in v0.0.2

func (h *Hub) GetSubscribers(channel string) []*Client

Return client list subscribed to channel

func (*Hub) Identify added in v0.0.2

func (h *Hub) Identify() et.Json

Identify Hub

func (*Hub) Mute added in v0.0.2

func (h *Hub) Mute(channel string, msg Message, ignored []string, from et.Json) error

Publish mute a message to a channel less the ignore client

func (*Hub) Publish

func (h *Hub) Publish(channel string, msg Message, ignored []string, from et.Json) error

Publish a message to a channel less the ignore client

func (*Hub) RedisAdapter added in v0.0.2

func (h *Hub) RedisAdapter(params *RedisAdapterParams) error

Set a adapter to make a cluster hub

func (*Hub) Run added in v0.0.2

func (h *Hub) Run()

Run the hub

func (*Hub) SendMessage

func (h *Hub) SendMessage(clientId string, msg Message) error

Send a message to a client in a channel

func (*Hub) SetName

func (h *Hub) SetName(name string)

SetAtribs set a value to the client data

func (*Hub) Stack

func (h *Hub) Stack(clientId string, channel string) error

Subscribe a client to hub channels

func (*Hub) Subscribe

func (h *Hub) Subscribe(clientId string, channel string) error

Subscribe a client to hub channels

func (*Hub) Unsubscribe

func (h *Hub) Unsubscribe(clientId string, channel string) error

Unsubscribe a client from hub channels

type HubParams added in v0.0.2

type HubParams struct {
	Id   string
	Name string
}

type Message

type Message struct {
	Created_at time.Time `json:"created_at"`
	Id         string    `json:"id"`
	From       et.Json   `json:"from"`

	Ignored []string    `json:"ignored"`
	Tp      m.TpMessage `json:"tp"`
	Channel string      `json:"channel"`
	Data    interface{} `json:"data"`
	// contains filtered or unexported fields
}

func DecodeMessage

func DecodeMessage(data []byte) (Message, error)

Decode return the message as struct

func NewMessage

func NewMessage(from et.Json, message interface{}, tp m.TpMessage) Message

NewMessage create a new message

func (Message) Encode

func (e Message) Encode() ([]byte, error)

Encode return the message as byte

func (Message) Json added in v0.0.2

func (e Message) Json() (et.Json, error)

func (Message) ToString

func (e Message) ToString() string

ToString return the message as string

func (Message) Type

func (e Message) Type() m.TpMessage

Type return the type of message

type MessageBroadcast added in v0.0.2

type MessageBroadcast struct {
	Kind    TpBroadcast `json:"kind"`
	To      string      `json:"channel"`
	Msg     Message     `json:"msg"`
	Ignored []string    `json:"ignored"`
	From    et.Json     `json:"from"`
}

MessageBroadcast is a struct to manage the message to broadcast

func (MessageBroadcast) Encode added in v0.0.2

func (m MessageBroadcast) Encode() ([]byte, error)

Encode return the message as byte

type PubSub added in v0.0.2

type PubSub struct {
	ClientId string
	Name     string
	// contains filtered or unexported fields
}

func NewPubSub added in v0.0.2

func NewPubSub() (*PubSub, error)

Create a new client websocket connection

func (*PubSub) Close added in v0.0.2

func (p *PubSub) Close()

Close the client websocket connection

func (*PubSub) Connect added in v0.0.2

func (p *PubSub) Connect() (bool, error)

Connect to the server

func (*PubSub) IsConnected added in v0.0.2

func (p *PubSub) IsConnected() bool

Check if the client is connected

func (*PubSub) Params added in v0.0.2

func (p *PubSub) Params(params et.Json) error

Set the client parameters

func (*PubSub) Ping added in v0.0.2

func (p *PubSub) Ping()

Ping the server

func (*PubSub) Publish added in v0.0.2

func (p *PubSub) Publish(channel string, message interface{})

Publish a message to a channel

func (*PubSub) SendMessage added in v0.0.2

func (p *PubSub) SendMessage(clientId string, message interface{}) error

Send a message to the server

func (*PubSub) Stack added in v0.0.2

func (p *PubSub) Stack(channel string, reciveFn func(m.Message))

Subscribe to a channel type fisrt, so send message to first client

func (*PubSub) Subscribe added in v0.0.2

func (p *PubSub) Subscribe(channel string, reciveFn func(m.Message))

Subscribe to a channel

func (*PubSub) Type added in v0.0.2

func (p *PubSub) Type() string

Return type server pubsub

func (*PubSub) Unsubscribe added in v0.0.2

func (p *PubSub) Unsubscribe(channel string)

Unsubscribe from a channel

type RedisAdapter added in v0.0.2

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

func NewRedisAdapter

func NewRedisAdapter(params *RedisAdapterParams) (*RedisAdapter, error)

NewRedisAdapter create a new RedisAdapter

func (*RedisAdapter) Broadcast added in v0.0.2

func (a *RedisAdapter) Broadcast(to string, msg Message, ignored []string, from et.Json) error

Broadcast a message to all clients in the cluster hub

func (*RedisAdapter) Close added in v0.0.2

func (a *RedisAdapter) Close() error

Close the connection

func (*RedisAdapter) Command added in v0.0.2

func (a *RedisAdapter) Command(command string, params et.Json) error

func (*RedisAdapter) Direct added in v0.0.2

func (a *RedisAdapter) Direct(to string, msg Message) error

Broadcast a message to all clients in the cluster hub

type RedisAdapterParams added in v0.0.2

type RedisAdapterParams struct {
	Addr     string
	Password string
	DB       int
}

* * RedisAdapter is a struct to manage the Redis connection * to broadcast messages to all clients in the cluster hub *

type TpBroadcast added in v0.0.2

type TpBroadcast int
const (
	TpAll TpBroadcast = iota
	TpRoundRobin
	TpDirect
	TpCommand
)

func (TpBroadcast) String added in v0.0.2

func (tp TpBroadcast) String() string

String return the string representation of the broadcast type

type WsMessage

type WsMessage struct {
	Type    string      `json:"type"`
	Payload interface{} `json:"payload"`
}

Jump to

Keyboard shortcuts

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