Documentation ¶
Overview ¶
Package chatsrv implements a simple chat server.
Index ¶
- Constants
- func NewServer(config *ServerConfig) *server
- type Client
- func (client *Client) FriendlyName() string
- func (client *Client) GetVar(varName string) interface{}
- func (client *Client) InputMode() InputMode
- func (client *Client) SetFriendlyName(friendlyName string)
- func (client *Client) SetInputMode(inputMode InputMode) error
- func (client *Client) SetVar(varName string, value interface{})
- func (client *Client) Stop(reason string)
- func (client *Client) Stopped() bool
- func (client *Client) StoppedReason() string
- func (client *Client) String() string
- func (client *Client) UnsetVar(varName string)
- func (client *Client) Uuid() uuid.UUID
- func (client *Client) VarExists(varName string) bool
- type ClientHandler
- type ClientHandlerFunc
- type InputMode
- type ServerConfig
Constants ¶
const ( InputModeLines = InputMode(iota) InputModeBytes InputModeRunes )
const SendBuffSize = 10 // Buffer size of channel for sending data to clients
Variables ¶
This section is empty.
Functions ¶
func NewServer ¶
func NewServer(config *ServerConfig) *server
NewServer creates a new server with the specified configuration
Types ¶
type Client ¶
type Client struct { Send chan []byte // Bytes sent here will be written to the client Recv chan []byte // Bytes received by client will be sent here. // contains filtered or unexported fields }
Represents a client on the server. It is the ClientHandler's responsibility to make sure nothing sends on client.Send after the client is stopped, as client.Send will be closed.
func NewClient ¶
func NewClient(rw io.ReadWriteCloser, inputMode InputMode, clientHandler ClientHandler) (*Client, error)
NewClient initializes a new client, and starts the initial client handler. When the initial ClientHandler stops, the client will be disconnected.
func (*Client) FriendlyName ¶
FriendlyName Gets the client's friendly name. This is how the client will be identified when converted to a string.
func (*Client) SetFriendlyName ¶
SetFriendlyName specifies how the client should identify itself when being converted to a string. This replaces the default, which is the client's UUID. Set this to "" to restore the default.
func (*Client) SetInputMode ¶
func (*Client) Stop ¶
Stop stops a client, closing it's ReadWriteCloser. Stop is idempotent; calling Stop more than once will have no effect. The send channel will not be closed until the initial ClientHandler returns.
func (*Client) StoppedReason ¶
StoppedReason returns the reason the client was stopped. Returns "" if the client is still running, or if no reason was set.
type ClientHandler ¶
ClientHandler provides the server's end of the conversation with a client. For example, it might echo text they type back to them, or patch them into a chat.
ClientHandlers can call other ClientHandlers, but will be blocked until the called handler returns. Receive from client.Recv, and respond to client.Send. When client.Stop(reason string) is called, client.Send is closed. It is therefore a good idea to make sure client.Stopped() is false when regaining control from a called ClientHandler to prevent panics.
type ClientHandlerFunc ¶
ClientHandlerFunc is an adapter to use an ordinary function as a ClientHandler. If f is a func(*Client) string, ClientHandlerFunc(f) is a ClientHandler whose Handle() method calls f.
func (ClientHandlerFunc) Handle ¶
func (f ClientHandlerFunc) Handle(client *Client) string
Handle calls f(client)