Documentation ¶
Index ¶
- Constants
- Variables
- func AllocateColors()
- func HandlerChat(conn *websocket.Conn)
- func PopColor() colorful.Color
- func SendEvent(conn *websocket.Conn, ev Event)
- func SendEventClient(conn *websocket.Conn, ev Event) error
- type Event
- type Server
- func (s *Server) HasID(id int) bool
- func (s *Server) Join(user *User) error
- func (s *Server) Leave(user *User)
- func (mj *Server) MarshalJSON() ([]byte, error)
- func (mj *Server) MarshalJSONBuf(buf fflib.EncodingBuffer) error
- func (s *Server) Start() error
- func (s *Server) Stop()
- func (s *Server) TryNewTopic(t Topic) error
- func (uj *Server) UnmarshalJSON(input []byte) error
- func (uj *Server) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error
- type Topic
- type User
- func (u *User) HandleLeave(event Event)
- func (u *User) HandleLogin(event Event)
- func (u *User) HandleMessage(event Event)
- func (u *User) HandleNewTopic(event Event)
- func (u *User) HandleVote(event Event)
- func (u *User) ListenEvents()
- func (mj *User) MarshalJSON() ([]byte, error)
- func (mj *User) MarshalJSONBuf(buf fflib.EncodingBuffer) error
- func (uj *User) UnmarshalJSON(input []byte) error
- func (uj *User) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error
Constants ¶
const ( EVENT_LOGIN = "login" EVENT_MESSAGE = "message" EVENT_LEAVE = "leave" EVENT_VOTE = "vote" EVENT_ERROR = "error" EVENT_STATUS = "status" EVENT_NEWTOPIC = "newtopic" )
const ( VOTE_OPTION_A voteOption = 1 << iota VOTE_OPTION_B )
const MaxTopics = 3
Variables ¶
var GlobalServer = NewServer()
Functions ¶
func HandlerChat ¶
HandlerChat is called in a goroutine for every connection the websocket accepts.
func PopColor ¶
func PopColor() colorful.Color
PopColor returns a new color from our color stack. If we run out of colors and fail to allocate a new batch we will return an error
Types ¶
type Event ¶
type Event struct { // global Type string `json:"type"` // login Username string `json:"username,omitempty"` Color string `json:"color,omitempty"` // message Text string `json:"text,omitempty"` TopicID int `json:"topic-id,omitempty"` // newtopic OptionA string `json:"option-a"` OptionB string `json:"option-b"` // vote // notice: this UPDATES the mask. VoteMask voteOption `json:"vote-mask,omitempty"` Error string `json:"error,omitempty"` // contains filtered or unexported fields }
Event is a set of data that will be sent over the websocket
See events.md for more information
func NewErrorEvent ¶
NewErrorEvent returns an Event with the error field filled for you
func (*Event) MarshalJSON ¶
func (*Event) MarshalJSONBuf ¶
func (mj *Event) MarshalJSONBuf(buf fflib.EncodingBuffer) error
func (*Event) UnmarshalJSON ¶
func (*Event) UnmarshalJSONFFLexer ¶
type Server ¶
type Server struct { Users map[string]*User `json:"-"` Topics map[int]*Topic `json:"users"` Errors chan error `json:"-"` Kill chan struct{} `json:"-"` Events chan Event `json:"-"` // contains filtered or unexported fields }
Server represents a single chat server that will run
func NewServer ¶
func NewServer() *Server
NewServer creates a new Server with all of the fieldsd initialized
func (*Server) MarshalJSON ¶
func (*Server) MarshalJSONBuf ¶
func (mj *Server) MarshalJSONBuf(buf fflib.EncodingBuffer) error
func (*Server) Stop ¶
func (s *Server) Stop()
Stop will attempt to gracely stop the server and close the topic
func (*Server) TryNewTopic ¶
TryNewTopic attempts to create a new topic if we have less than three topics.
This method takes advantage of a sync.Mutex
func (*Server) UnmarshalJSON ¶
func (*Server) UnmarshalJSONFFLexer ¶
type Topic ¶
type Topic struct { OptionA string `json:"option-a"` OptionB string `json:"option-b"` ID int `json:"id"` Created time.Time `json:"created"` Ends time.Time `json:"ends"` // contains filtered or unexported fields }
Topic is a single topic that can be ran on a server
func (*Topic) MarshalJSON ¶
func (*Topic) MarshalJSONBuf ¶
func (mj *Topic) MarshalJSONBuf(buf fflib.EncodingBuffer) error
func (*Topic) UnmarshalJSON ¶
func (*Topic) UnmarshalJSONFFLexer ¶
type User ¶
type User struct { Name string `json:"name"` Color string `json:"color"` Remote string `json:"-"` Events chan Event `json:"-"` Kill chan struct{} `json:"-"` LoggedIn bool `json:"logged-in"` Votes map[int]voteOption `json:"voteMask"` // contains filtered or unexported fields }
User is a single user that can be connected to a user When a user joins a channel a goroutine is started to listen for data on the websocket
func (*User) HandleLeave ¶
func (*User) HandleLogin ¶
func (*User) HandleMessage ¶
func (*User) HandleNewTopic ¶
func (*User) HandleVote ¶
func (*User) ListenEvents ¶
func (u *User) ListenEvents()
ListenEvents starts a goroutine to read for events. If stops if you send anything to User.Kill
func (*User) MarshalJSON ¶
func (*User) MarshalJSONBuf ¶
func (mj *User) MarshalJSONBuf(buf fflib.EncodingBuffer) error