Documentation ¶
Overview ¶
Package server implements a chat server for websocket access.
Index ¶
Constants ¶
const ( ChatReqTypeSetNickname = 101 + iota ChatReqTypeGetNickname ChatReqTypeListRooms ChatReqTypeJoin ChatReqTypeListNames ChatReqTypeHide ChatReqTypeUnhide ChatReqTypeMsg ChatReqTypeLeave )
const ( ChatRspTypeSetNickname = 101 + iota ChatRspTypeGetNickname ChatRspTypeListRooms ChatRspTypeJoin ChatRspTypeListNames ChatRspTypeHide ChatRspTypeUnhide ChatRspTypeMsg ChatRspTypeLeave )
const ( ChatRspTypeErrRoomMandatory = 1001 + iota ChatRspTypeErrMaxRoomsReached ChatRspTypeErrNicknameMandatory ChatRspTypeErrAlreadyJoined ChatRspTypeErrNicknameUsed ChatRspTypeErrHiddenNickname ChatRspTypeErrUnknownReq )
const ( DefaultHostname = "localhost" // The hostname of the server. DefaultPort = 6660 // Port to receive requests: see IANA Port Numbers. DefaultProfPort = 0 // Profiler port to receive requests. * DefaultMaxConns = 0 // Maximum number of connections allowed. * DefaultMaxRooms = 0 // Maximum number of chat rooms allowed. * DefaultMaxIdle = 0 // Maximum idle seconds per user connection. * DefaultMaxProcs = 0 // Maximum number of computer processors to utilize. * )
Variables ¶
This section is empty.
Functions ¶
func PrintUsageAndExit ¶
func PrintUsageAndExit()
PrintUsageAndExit is used to print out command line options.
func PrintVersionAndExit ¶
func PrintVersionAndExit()
PrintVersionAndExit prints the version of the server then exits.
Types ¶
type ChatLogger ¶
ChatLogger is an enhancement over the base logger for application logging patterns.
func ChatLoggerNew ¶
func ChatLoggerNew() *ChatLogger
ChatLoggerNew is a factory function that returns a new ChatLogger instance.
func (*ChatLogger) LogConnect ¶
func (l *ChatLogger) LogConnect(r *http.Request)
LogConnect is used to log request information when the client first connects to the server.
func (*ChatLogger) LogError ¶
func (l *ChatLogger) LogError(addr string, msg string)
LogError is used to record misc session error information between server and client.
func (*ChatLogger) LogSession ¶
func (l *ChatLogger) LogSession(tp string, addr string, msg string)
LogSession is used to record information received during the client's session.
type ChatManager ¶ added in v0.1.2
type ChatManager struct {
// contains filtered or unexported fields
}
ChatManager represents a control hub of chat rooms and chatters for the server.
func ChatManagerNew ¶ added in v0.1.2
func ChatManagerNew(maxr int, maxi int, l *ChatLogger) *ChatManager
ChatManagerNew is a factory function that returns a new instance of a chat manager.
func (*ChatManager) MaxIdle ¶ added in v0.1.2
func (m *ChatManager) MaxIdle() int
MaxIdle returns the current maximum idle time for a connection.
func (*ChatManager) MaxRooms ¶ added in v0.1.2
func (m *ChatManager) MaxRooms() int
MaxRooms returns the current maximum number of rooms allowed on the server.
func (*ChatManager) SetMaxIdle ¶ added in v0.1.2
func (m *ChatManager) SetMaxIdle(maxi int)
SetMaxIdle sets the maximum idle time for a connection.
func (*ChatManager) SetMaxRooms ¶ added in v0.1.2
func (m *ChatManager) SetMaxRooms(maxr int)
SetMaxRooms sets the maximum number of rooms allowed on the server.
type ChatRequest ¶
type ChatRequest struct { Who *Chatter `json:"-"` // The chatter who is issuing the request. RoomName string `json:"roomName"` // The name of the room to receive the request. ReqType int `json:"reqType"` // The command type ex: join, leave, send. Content string `json:"content"` // Any message or text to interpret with the request. }
ChatRequest is a structure for commands sent for processing from the client.
func ChatRequestNew ¶
ChatMessageNew is a factory method that returns a new chat room message instance.
func (*ChatRequest) String ¶
func (r *ChatRequest) String() string
String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.
type ChatResponse ¶
type ChatResponse struct { RoomName string `json:"roomName"` // The room name where the response originated. RspType int `json:"rspType"` // The response type ex: join, leave, send. Content string `json:"content"` // Any message text or other content for the client. List []string `json:"list"` // A list of entries returned with the response. }
ChatResponse is a structure for JSON responses sent back to the client.
func ChatResponseNew ¶
ChatResponseNew is a factory method that returns a new chat room message instance.
func (*ChatResponse) String ¶
func (r *ChatResponse) String() string
String is an implentation of the Stringer interface so the structure is returned as a string to fmt.Print() etc.
type ChatRoom ¶
type ChatRoom struct {
// contains filtered or unexported fields
}
ChatRoom represents a hub of chatters where messages can be exchanged.
func ChatRoomNew ¶
ChatRoomNew is a factory function that returns a new instance of a chat room.
func (*ChatRoom) ChatRoomStatsNew ¶ added in v0.1.1
func (r *ChatRoom) ChatRoomStatsNew() *ChatRoomStats
ChatRoomStatsNew returns status information on the room.
type ChatRoomChatterStat ¶
type ChatRoomStats ¶
type ChatRoomStats struct { Name string `json:"name"` // The name of the room. Start time.Time `json:"start"` // The start time of the room. LastReq time.Time `json:"lastReq"` // The last request time to the room. LastRsp time.Time `json:"lastRsp"` // The last response time from the room. ReqCount uint64 `json:"reqcount"` // Total requests received. RspCount uint64 `json:"rspCount"` // Total responses sent. Chatters []*ChatRoomChatterStat `json:"chatters"` // Stats on chatters in the room }
ChatRoomStats is a simple structure for returning statistic information on the room.
type Chatter ¶
type Chatter struct {
// contains filtered or unexported fields
}
Chatter is a wrapper around a connection that represents one chat client on the server.
func ChatterNew ¶
func ChatterNew(cm *ChatManager, w *websocket.Conn, l *ChatLogger) *Chatter
ChatterNew is a factory function that returns a new Chatter instance
func (*Chatter) ChatterStatsNew ¶ added in v0.1.1
func (c *Chatter) ChatterStatsNew() *ChatterStats
ChatterStatsNew returns status information on the chatter.
type ChatterStats ¶
type ChatterStats struct { Nickname string `json:"nickname"` // The nickname of the chatter. RemoteAddr string `json:"remoteAddr"` // The remote IP and port of the chatter. Start time.Time `json:"start"` // The start time of the chatter. LastReq time.Time `json:"lastReq"` // The last request time from the chatter. LastRsp time.Time `json:"lastRsp"` // The last response time to the chatter. ReqCount uint64 `json:"reqcount"` // Total requests received. RspCount uint64 `json:"rspCount"` // Total responses sent. }
ChatterStats is a simple structure for returning statistic information on the chatter.
type Info ¶
type Info struct { Version string `json:"version"` // Version of the server. Name string `json:"name"` // The name of the server. Hostname string `json:"hostname"` // The hostname of the server. UUID string `json:"UUID"` // Unique ID of the server. Port int `json:"port"` // Port the server is listening on. ProfPort int `json:"profPort"` // Profiler port the server is listening on. MaxConns int `json:"maxConns"` // The maximum concurrent clients accepted. MaxRooms int `json:"maxRooms"` // The maximum number of chat rooms allowed. MaxIdle int `json:"maxIdle"` // The maximum client idle time in seconds before disconnect. Debug bool `json:"debugEnabled"` // Is debugging enabled on the server. }
Info provides basic config information to/about the running server.
type Options ¶
type Options struct { Name string `json:"name"` // The name of the server. Hostname string `json:"hostname"` // The hostname of the server. Port int `json:"port"` // The default port of the server. ProfPort int `json:"profPort"` // The profiler port of the server. MaxConns int `json:"maxConns"` // The maximum concurrent clients accepted. MaxRooms int `json:"maxRooms"` // The maximum number of chat rooms allowed. MaxIdle int `json:"maxIdle"` // The maximum client idle time in seconds before disconnect. MaxProcs int `json:"maxProcs"` // The maximum number of processor cores available. Debug bool `json:"debugEnabled"` // Is debugging enabled in the application or server. }
Options represents parameters that are passed to the application to be used in constructing the server.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the main structure that represents a server instance.
func (*Server) Shutdown ¶
func (s *Server) Shutdown()
Shutdown takes down the server gracefully back to an initialize state.
func (*Server) StartProfiler ¶
func (s *Server) StartProfiler()
StartProfiler is called to enable dynamic profiling.
type Stats ¶
type Stats struct { Start time.Time `json:"startTime"` // The start time of the server. ReqCount int64 `json:"reqCount"` // How many requests came in to the server. ReqBytes int64 `json:"reqBytes"` // Size of the requests in bytes. RouteStats map[string]map[string]int64 `json:"routeStats"` // How many requests/bytes came into each route. ChatterStats []*ChatterStats `json:"chatterStats"` // Statistics about each logged in chatter. RoomStats []*ChatRoomStats `json:"roomStats"` // How many requests etc came into each room. }
Stats contains runtime statistics for the server.
func StatsNew ¶
StatsNew is a factory function that returns a new instance of statistics. options is an optional list of functions that initialize the structure
func (*Stats) IncrReqStats ¶
IncrReqStats increments the stats totals for the server.
func (*Stats) IncrRouteStats ¶
IncrRouteStats increments the stats totals for the route.