socketify

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 7 Imported by: 0

README

Go-Socketify

A simple WebSocket framework for Go

Install

go get -u github.com/aliforever/go-socketify

Usage

A simple app that PONG when PING

options := socketify.Options().SetAddress(":8080").SetEndpoint("/ws").IgnoreCheckOrigin()
server := socketify.New(options)
go server.Listen()

for client := range server.Clients() {
    client.HandleUpdate("PING", func(message json.RawMessage) {
        client.WriteUpdate("PONG", nil)
    })
    go client.ProcessUpdates()
}

Run the application and send below JSON to "ws://127.0.0.1:8080/ws":

{
  "type": "PING"
}

You'll receive:

{
  "type": "PONG"
}

Conventions

Events are ought to be sent/received with following JSON format:

{
  "type": "UpdateType",
  "data": {}
}

Type is going to be your update type and data is going to be anything.

Storage

You can retrieve clients within other clients by using Socketify's client storage.

You can enable the storage by setting option EnableStorage():

options := socketify.Options().
	SetAddress(":8080").
	SetEndpoint("/ws").
	IgnoreCheckOrigin().
	EnableStorage() // <-- This LINE

Each client has a unique ID set by shortid package, you can recall using client.ID().

Clients are stored in a map with their unique ID and you can retrieve them by calling:

client.Server().Storage().GetClientByID(UniqueID)

Handlers

You can specify a handler for an updateType to each client by using:

client.HandleUpdate("UpdateType", func(message json.RawMessage) {
	// Process message
})

This way Socketify will call your registered handler if it receives any updates with UpdateType specified.

Or you can just listen on updates on your own:

go client.ProcessUpdates()
go func(c *socketify.Client) {
    for update := range c.Updates() {
        fmt.Println(update)
    }
}(client)

Note: You should always call go client.ProcessUpdates() to let a Socketify client receive updates.

Docs:

Checkout Docs Here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Options

func Options() *options

Types

type Client

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

func (*Client) Close added in v0.1.4

func (c *Client) Close() error

func (*Client) CloseChannel

func (c *Client) CloseChannel() chan bool

func (*Client) GetAttribute added in v0.1.7

func (c *Client) GetAttribute(key string) (val string, exists bool)

func (*Client) HandleRawUpdate added in v0.1.3

func (c *Client) HandleRawUpdate(handler func(message json.RawMessage))

HandleRawUpdate registers a default handler for update Note: Add a raw handler if you don't want to follow the API convention {"type": "", "data": {}}

func (*Client) HandleUpdate

func (c *Client) HandleUpdate(updateType string, handler func(message json.RawMessage))

HandleUpdate registers a default handler for updateType Care: If you use this method for an updateType, you won't receive the respected update in your listener

func (*Client) ID

func (c *Client) ID() string

func (*Client) ProcessUpdates

func (c *Client) ProcessUpdates() (err error)

func (*Client) Server

func (c *Client) Server() *Socketify

func (*Client) SetAttribute added in v0.1.7

func (c *Client) SetAttribute(key, val string)

func (*Client) Updates

func (c *Client) Updates() chan *Update

func (*Client) UpgradeRequest added in v0.1.2

func (c *Client) UpgradeRequest() *http.Request

func (*Client) WriteRawUpdate added in v0.1.4

func (c *Client) WriteRawUpdate(data interface{})

func (*Client) WriteUpdate

func (c *Client) WriteUpdate(updateType string, data interface{})

type Logger

type Logger interface {
	Error(args ...interface{})
}

type Socketify

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

func New

func New(opts *options) (s *Socketify)

func (*Socketify) Clients

func (s *Socketify) Clients() chan *Client

func (*Socketify) Listen

func (s *Socketify) Listen() (err error)

func (*Socketify) Server

func (s *Socketify) Server() (server *http.Server)

func (*Socketify) Storage

func (s *Socketify) Storage() *storage

type Update

type Update struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data,omitempty"`
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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