ignite

package module
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 License: MIT Imports: 10 Imported by: 0

README

ignite

A websocket server module. Require redis to scale to multi nodes.

Client/server message follow format

type Message struct {
	Event   string          `json:"event"`
	Payload json.RawMessage `json:"payload"`
}

use module like the code below.

func main() {

	hub := ignite.NewServer(&ignite.ServerConfig{
		Namespace: "default",
		Address:   "localhost:8082",
		Path:      "/",
		RedisHost: "localhost",
		RedisPort: 6379,
		RedisDb:   10,
	})

	hub.OnNewClient(func(client *ignite.Client) {

		// Send indentity message
		client.SendId()

		// Join a room
		client.Join("#Go")

		// Send to a room except some client
		client.SendMsgExcept("#Go", []string{client.ID}, ignite.Message{
			Event:   "NEW_MEMBER",
			Payload: ignite.ToPayload(client.ID),
		})

		// Register handle for an event
		client.On("BUY", "1", func(payload json.RawMessage) {
			client.SendMsg(ignite.Message{
				Event:   "BUY_RESPONSE",
				Payload: payload,
			})

		})
		client.OnClose(func(reason string) {
			fmt.Println("Client ", client.ID, " closed: ", reason)
		})
	})

	forever := make(chan struct{})
	<-forever
}

send/received message

❯ wscat -c "ws://localhost:8081/"
Connected (press CTRL+C to quit)
< {"event":"identity","payload":{"clientId":"1ddcc65d-49ab-496a-9939-5da768d1c52c"}}
> {"event":"BUY","payload":"VCS"}
< {"event":"BUY_RESPONSE","payload":"VCS"}
>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToPayload added in v1.0.11

func ToPayload(value interface{}) json.RawMessage

Types

type Client

type Client struct {

	// Identity
	ID string
	// contains filtered or unexported fields
}

Client is a middleman between the websocket connection and the hub.

func (*Client) BroadcastMsg

func (c *Client) BroadcastMsg(msg Message)

Send message to all active connection

func (*Client) Get added in v1.0.12

func (c *Client) Get(key string) interface{}

retrieve value by given key from client metadata

func (*Client) Join

func (c *Client) Join(roomId string)

Join a room

func (*Client) Leave

func (c *Client) Leave(roomId string)

Leave a room

func (*Client) Off

func (c *Client) Off(event string, funcId string)

Unregister handle func for an event

func (*Client) On

func (c *Client) On(event string, funcId string, f func(json.RawMessage))

Register handle func for an event

func (*Client) OnClose

func (c *Client) OnClose(f func(string))

Callback function when a client closed connection

func (*Client) RawMessage added in v1.0.12

func (c *Client) RawMessage(payload interface{}) []byte

marshall message to json.RawMessage

func (*Client) SendId added in v1.0.11

func (c *Client) SendId()

Send id to client

func (*Client) SendMsg added in v1.0.8

func (c *Client) SendMsg(message Message)

Send direct message to client

func (*Client) SendMsgExcept added in v1.0.11

func (c *Client) SendMsgExcept(roomId string, exclude_ids []string, message Message)

Send message to specific room except some client

func (*Client) SendMsgToRoom

func (c *Client) SendMsgToRoom(roomId string, message Message)

Send message to specific room

func (*Client) Set added in v1.0.12

func (c *Client) Set(key string, value interface{})

set a value to client metadata

type Hub

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

Hub maintains the set of active clients and broadcasts messages to the clients.

func NewServer

func NewServer(config *ServerConfig) *Hub

func (*Hub) BroadcastMsg

func (h *Hub) BroadcastMsg(msg Message)

func (*Hub) OnNewClient

func (h *Hub) OnNewClient(fn func(client *Client))

func (*Hub) SendMsgExcept added in v1.0.11

func (h *Hub) SendMsgExcept(roomId string, exclude_ids []string, message Message)

Send message to specific room except some client

func (*Hub) SendMsgToRoom

func (h *Hub) SendMsgToRoom(roomId string, message Message)

Send message to specific room

type Message

type Message struct {
	Event   string          `json:"event"`
	Payload json.RawMessage `json:"payload"`
}

Client/Server message format

type ServerConfig added in v1.0.11

type ServerConfig struct {
	Namespace     string
	Address       string
	Path          string
	RedisHost     string
	RedisPort     uint
	RedisPassword string
	RedisDb       uint
}

Jump to

Keyboard shortcuts

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