server

package
v0.0.0-...-a5a3fa4 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleConnections

func HandleConnections(s *Server) func(w http.ResponseWriter, r *http.Request)

HandleConnections returns a httpHandlerfunc for ever client that joins it upgrades the connection to a socket connection Registers the client in *Server.Clients Writes time elapsed pings to the clients Listens for Operation Messages from clients and executes them

func NewChallengeSub

func NewChallengeSub(s *Server) func(pubSubChan <-chan *redis.Message)

NewChallengeSub subscribes to challenges.new channel in redis. It sends recieved values to all connected clients

func Ping

func Ping(client *Client, request Request) error

func PublishToRedis

func PublishToRedis(s *Server) func(client *Client, request Request) error

PubslishToredis is a temporary command When Client ssends {'op':'1':'d':{somedata}} it publishes the data to the "challenges.new" channel

func RedisHandler

func RedisHandler(s *Server)

RedisHandler subscribes too all registered Redis Channels and runs the respective handlers in goroutines

Types

type Client

type Client struct {
	Mu sync.Mutex
	Ws *websocket.Conn
}

Client is a struct for a single Client connected through sockets Mu sync.Mutex is to prevent concurrent writing to Ws *websocket.Conn Ws *websocket.Conn is a gorilla/websocket upgraded connection to the client

type Request

type Request struct {
	OperationCode string      `json:"op"`
	Data          interface{} `json:"d"`
}

type Server

type Server struct {
	Mu            sync.Mutex
	Clients       map[*Client]bool //
	RedisClient   *redis.Client
	Operations    map[string]func(client *Client, request Request) error
	Config        utils.Config
	RedisHandlers map[string]func(pubSubChan <-chan *redis.Message)
}

Server struct is the core of this application It has 6 properties Mu -> sync.Mutex is to prevent concurrent writing to Clients Clients map[*Client]bool is a map of Client structs connected through sockets Operations are all the operations available to the clients (ex: ping, authenticate, publish) Operations have a code like 1, 2, ... RedisHandlers is a map of all RedisHandlers for specific RedisChannels that the application is subscribed to

func CreateServer

func CreateServer() *Server

CreateServer is returns a ready to use *Server Usage server := CreateServer()

func (*Server) RegisterCommand

func (s *Server) RegisterCommand(opCode string, callback func(client *Client, request Request) error) error

RegisterCommand is a method of s *Server It is used to safely add Operations *Server.Operations to the server. If there already exists a command with the operation code It returns an error Usage: err := s.RegisterCommand("1", Myfunc)

if err != nil{
	log.Fatalln(err)
}

func (*Server) RegisterCommands

func (s *Server) RegisterCommands() error

func (*Server) RegisterRedisHandler

func (s *Server) RegisterRedisHandler(channelName string, handler func(pubSubChan <-chan *redis.Message)) error

RegisterRedisHandler is a method of s *Server It is used to safely add Redis Handlers func(pubSubChan <- chan *redis.Message) to the server. If there already exists a command with the operation code It returns an error Usage: err := s.RegisterRedisHandler("myChannel", Myfunc)

if err != nil{
	log.Fatalln(err)
}

func (*Server) RegisterRedisHandlers

func (s *Server) RegisterRedisHandlers() error

func (*Server) RunServer

func (s *Server) RunServer(host string, port int) (err error)

func (*Server) UseCommand

func (s *Server) UseCommand(opCode string) (func(client *Client, request Request) error, error)

UseCommand is used in HandleConnections When HandleConnections recieves an operation code from the client It uses UseCommand to return the corresponding function for the operation code

type SubEvent

type SubEvent struct {
	Event string                 `json:"event"`
	Data  map[string]interface{} `json:"d"`
}

Jump to

Keyboard shortcuts

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