...

Package ws

import "github.com/Diarkis/diarkis/server/ws"
Overview
Index

Overview ▾

Constants

Bad Response packet status

const Bad = 4

Err Response packet status

const Err = 5

Ok Response packet status

const Ok = 1

Type Mesh network type for WebSocket server

const Type = "WS"

func Command

func Command(ver float64, cmd float64, handlerName string, handler func(*State, func(error)))

Command Registers a packet handler as a command

func GetConnectionTTL

func GetConnectionTTL() int64

GetConnectionTTL returns connection TTL

func GetEndPoint

func GetEndPoint() string

GetEndPoint Returns WS server end point that is actually bound

func HookAllCommands

func HookAllCommands(handlerName string, handler func(*State, func(error)))

HookAllCommands Registers a packet handler as a hook to all commands

func Init

func Init()

Init [INTERNAL USE ONLY] Flag the WS server state to be initialized to prevent registration of commands and hooks BEFORE initialization by user package

func IsCommandDuplicate

func IsCommandDuplicate(ver float64, commandID float64) bool

IsCommandDuplicate returns true if ver and commandID are used elsewhere when this function is called

func Offline

func Offline()

Offline Sets mesh network status to offline

func OnDisconnect

func OnDisconnect(callback func(string, string, string, error), last bool)

OnDisconnect Registers a callback on connection disconnect

func OnHeartbeat

func OnHeartbeat(handler func(*State, func(error)))

OnHeartbeat Registers a handler to be executed on heartbeat

func OnNewConnection

func OnNewConnection(callback func(*State))

OnNewConnection Registers a callback on new WS connection w/ a client

func Online

func Online()

Online Sets mesh network status to online

func SetAllWriteHook

func SetAllWriteHook(handler func(float64, float64, *State, interface{}) (map[string]interface{}, error))

SetAllWriteHook [INTERNAL USE ONLY] Registers a handler function to all TCP socket writes: Receives the payload and returns a payload

func SetPublicEndPoint

func SetPublicEndPoint(addr string)

SetPublicEndPoint sets public endpoint to be used by the client to connect

func SetPublicEndPointWithPort

func SetPublicEndPointWithPort(addr string, port string)

SetPublicEndPointWithPort sets public endpoint with a port passed to be used by the client to connect

port string should be the port used by nginx or something similar for proxy

func Setup

func Setup(confpath string)

Setup [INTERNAL USE ONLY] Loads configuration file into memory - pass an empty string to load nothing

func ShutdownWS

func ShutdownWS(next func(error))

ShutdownWS Stops WebSocket server

type State

State Client state data structure

type State struct {
    UserID           string
    SID              string
    Conn             *websocket.Conn
    ClientAddr       string
    TTLDuration      int64
    TTL              int64
    Connected        bool
    Ver              float64
    Command          float64
    Payload          map[string]interface{}
    ClientKey        string
    EncryptionKey    []byte
    EncryptionIV     []byte
    EncryptionMacKey []byte
}

func GetStateByAddr

func GetStateByAddr(addr string) *State

GetStateByAddr Returns a client state struct by client address

func (*State) Disconnect

func (state *State) Disconnect()

Disconnect Disconnects the socket connection to the client

func (*State) Kill

func (state *State) Kill(err error)

Kill Forcefully terminates the socket connection to the client

func (*State) Send

func (state *State) Send(ver float64, cmd float64, payload interface{}) error

Send Sends a packet to the client