chat

package
v0.0.0-...-11cf8e9 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2015 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EVENT_LOGIN    = "login"
	EVENT_MESSAGE  = "message"
	EVENT_LEAVE    = "leave"
	EVENT_VOTE     = "vote"
	EVENT_ERROR    = "error"
	EVENT_STATUS   = "status"
	EVENT_NEWTOPIC = "newtopic"
)
View Source
const (
	VOTE_OPTION_A voteOption = 1 << iota
	VOTE_OPTION_B
)
View Source
const MaxTopics = 3

Variables

View Source
var GlobalServer = NewServer()

Functions

func AllocateColors

func AllocateColors()

AllocateColors returns a fresh batch of colors

func HandlerChat

func HandlerChat(conn *websocket.Conn)

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

func SendEvent

func SendEvent(conn *websocket.Conn, ev Event)

SendEvent will marshal an event to JSON and write it to the connection it passes all errors to the Server error channel

func SendEventClient

func SendEventClient(conn *websocket.Conn, ev Event) 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

func NewErrorEvent(err error) Event

NewErrorEvent returns an Event with the error field filled for you

func (*Event) MarshalJSON

func (mj *Event) MarshalJSON() ([]byte, error)

func (*Event) MarshalJSONBuf

func (mj *Event) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*Event) UnmarshalJSON

func (uj *Event) UnmarshalJSON(input []byte) error

func (*Event) UnmarshalJSONFFLexer

func (uj *Event) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

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) HasID

func (s *Server) HasID(id int) bool

HasID returns true if we have a topic with the given ID

func (*Server) Join

func (s *Server) Join(user *User) error

Join should be called when a user wants to join a server

func (*Server) Leave

func (s *Server) Leave(user *User)

Leave deletes the user instance from the server if it exists

func (*Server) MarshalJSON

func (mj *Server) MarshalJSON() ([]byte, error)

func (*Server) MarshalJSONBuf

func (mj *Server) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*Server) Start

func (s *Server) Start() error

Start will start all of the event passing logic

func (*Server) Stop

func (s *Server) Stop()

Stop will attempt to gracely stop the server and close the topic

func (*Server) TryNewTopic

func (s *Server) TryNewTopic(t Topic) error

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 (uj *Server) UnmarshalJSON(input []byte) error

func (*Server) UnmarshalJSONFFLexer

func (uj *Server) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

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 (mj *Topic) MarshalJSON() ([]byte, error)

func (*Topic) MarshalJSONBuf

func (mj *Topic) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*Topic) UnmarshalJSON

func (uj *Topic) UnmarshalJSON(input []byte) error

func (*Topic) UnmarshalJSONFFLexer

func (uj *Topic) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

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 (u *User) HandleLeave(event Event)

func (*User) HandleLogin

func (u *User) HandleLogin(event Event)

func (*User) HandleMessage

func (u *User) HandleMessage(event Event)

func (*User) HandleNewTopic

func (u *User) HandleNewTopic(event Event)

func (*User) HandleVote

func (u *User) HandleVote(event Event)

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 (mj *User) MarshalJSON() ([]byte, error)

func (*User) MarshalJSONBuf

func (mj *User) MarshalJSONBuf(buf fflib.EncodingBuffer) error

func (*User) UnmarshalJSON

func (uj *User) UnmarshalJSON(input []byte) error

func (*User) UnmarshalJSONFFLexer

func (uj *User) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

Jump to

Keyboard shortcuts

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