Documentation ¶
Index ¶
- Constants
- func EncodeJWT(c *ChatEvent, cr *ChatRoom, secretKey string) (tokenString string, err error)
- func ParseJWT(tokenString string, c *Claims, secretKey string) (err error)
- type APIError
- type Broker
- type ChatEvent
- type ChatRoom
- func (cr ChatRoom) AddClient(c *Client) (err error)
- func (cr ChatRoom) Authorize(c *ChatEvent) bool
- func (cr ChatRoom) IsValid() (err *APIError, validity bool)
- func (cr ChatRoom) MatchesPassword(val string) bool
- func (cr ChatRoom) Participants() int
- func (cr ChatRoom) PrettyTime() string
- func (cr ChatRoom) RemoveClient(user string) (err error)
- func (cr ChatRoom) ToJSON() (jsonEncoding []byte, err error)
- type ChatServer
- func (cs ChatServer) Add(cr *ChatRoom) (err error)
- func (cs ChatServer) Chats() (rooms []ChatRoom, err error)
- func (cs ChatServer) Delete(cr *ChatRoom) (err error)
- func (cs ChatServer) Init()
- func (cs ChatServer) Retrieve(title string) (cr *ChatRoom, err error)
- func (cs ChatServer) RetrieveID(ID int) (cr *ChatRoom, err error)
- func (cs ChatServer) Update(titleOrID string, modifiedChatRoom *ChatRoom) (err error)
- type Claims
- type Client
- type Outcome
Constants ¶
const ( // Subscribe is used to broadcast a message indicating user has joined ChatRoom Subscribe = "join" // Broadcast is used to broadcast messages to all subscribed users Broadcast = "send" // Unsubscribe is used to broadcast a message indicating user has left ChatRoom Unsubscribe = "leave" )
const ( // PublicRoom is a room open for anyone to join without authentication PublicRoom = "public" // PrivateRoom is password protected and requires an authentication token in order to process requests PrivateRoom = "private" // HiddenRoom is a private room that is not listed on public-facing APIs. TODO: Hide this from GET /chats/<id> as well? HiddenRoom = "hidden" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type APIError ¶
type APIError struct { Code int `json:"code,omitempty"` Msg string `json:"error,omitempty"` Field string `json:"field,omitempty"` }
APIError represents an error that was thrown at some point with some relevant information for users to correct their input
type Broker ¶
type Broker struct { // Registered Clients. Clients map[*Client]bool // Inbound messages from the Clients. Notification chan []byte // Register requests from the Clients. OpenClient chan *Client // Unregister requests from Clients. CloseClient chan *Client RoomID int }
Broker maintains the client connections and handles events using a listener goroutine
type ChatEvent ¶
type ChatEvent struct { EventType string `json:"event_type,omitempty"` User string `json:"name,omitempty"` RoomID int `json:"room_id,omitempty"` Color string `json:"color,omitempty"` Msg string `json:"msg,omitempty"` Password string `json:"secret,omitempty"` Timestamp time.Time `json:"time,omitempty"` }
ChatEvent represents a message event in an associated ChatRoom
func ValidateEvent ¶
ValidateEvent ensures data is a valid JSON representation of Chat Event and can be parsed as such
type ChatRoom ¶
type ChatRoom struct { Title string `json:"title"` Description string `json:"description,omitempty"` Type string `json:"visibility"` Password string `json:"password,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` ID int `json:"id"` Broker *Broker `json:"-"` Clients map[string]*Client `json:"-"` }
ChatRoom is a struct representing a chat room TODO: Add Administrator
func (ChatRoom) MatchesPassword ¶
MatchesPassword takes in a value and compares it with the room's password
func (ChatRoom) Participants ¶
Participants prints the # of active clients
func (ChatRoom) PrettyTime ¶
PrettyTime prints the creation date in a pretty format
func (ChatRoom) RemoveClient ¶
RemoveClient will remove a user from a ChatRoom
type ChatServer ¶
type ChatServer struct { RoomsID map[int]*ChatRoom Rooms map[string]*ChatRoom // TODO: Remove this duplication once data layer moves to DB Index *int }
ChatServer maintains all ChatRooms. TODO: This will be replaced by a database soon
var CS ChatServer = ChatServer{ RoomsID: make(map[int]*ChatRoom), Rooms: make(map[string]*ChatRoom), Index: &index, }
CS is the global ChatServer referencing all chat room objects
func (ChatServer) Add ¶
func (cs ChatServer) Add(cr *ChatRoom) (err error)
Add will create a new chat room and add it to the server
func (ChatServer) Chats ¶
func (cs ChatServer) Chats() (rooms []ChatRoom, err error)
Chats will return all non-hidden ChatRooms
func (ChatServer) Init ¶
func (cs ChatServer) Init()
Init will initialize the ChatServer with the default public room.
func (ChatServer) Retrieve ¶
func (cs ChatServer) Retrieve(title string) (cr *ChatRoom, err error)
Retrieve returns a single chat room based on title or ID
func (ChatServer) RetrieveID ¶
func (cs ChatServer) RetrieveID(ID int) (cr *ChatRoom, err error)
RetrieveID returns a single chat room based on ID. NOTE: This has no error handling unlike cs.Retrieve()
type Claims ¶
type Claims struct { Username string `json:"username"` RoomID int `json:"room_id,omitempty"` jwt.StandardClaims }
Claims is a model that represents JSON web tokens used for authentication by users
type Client ¶
type Client struct { Username string `json:"username"` Color string `json:"color"` LastActivity time.Time `json:"last_activity"` // The websocket Connection. Conn *websocket.Conn `json:"-"` // Buffered channel of outbound messages. Send chan []byte `json:"-"` // ChatRoom that client is registered with Room *ChatRoom `json:"-"` }
Client represents a user in a ChatRoom
func (*Client) ReadPump ¶
func (c *Client) ReadPump()
ReadPump pumps messages from the websocket connection to the broker.
The application runs ReadPump in a per-connection goroutine. The application ensures that there is at most one reader on a connection by executing all reads from this goroutine.
func (*Client) WritePump ¶
func (c *Client) WritePump()
WritePump pumps messages from the broker to the websocket connection.
A goroutine running WritePump is started for each connection. The application ensures that there is at most one writer to a connection by executing all writes from this goroutine.