Documentation
¶
Index ¶
- func HandleConnections(s *Server) func(w http.ResponseWriter, r *http.Request)
- func NewChallengeSub(s *Server) func(pubSubChan <-chan *redis.Message)
- func Ping(client *Client, request Request) error
- func PublishToRedis(s *Server) func(client *Client, request Request) error
- func RedisHandler(s *Server)
- type Client
- type Request
- type Server
- func (s *Server) RegisterCommand(opCode string, callback func(client *Client, request Request) error) error
- func (s *Server) RegisterCommands() error
- func (s *Server) RegisterRedisHandler(channelName string, handler func(pubSubChan <-chan *redis.Message)) error
- func (s *Server) RegisterRedisHandlers() error
- func (s *Server) RunServer(host string, port int) (err error)
- func (s *Server) UseCommand(opCode string) (func(client *Client, request Request) error, error)
- type SubEvent
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 PublishToRedis ¶
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 ¶
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 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 (*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 (*Server) UseCommand ¶
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