Documentation ¶
Overview ¶
Let me apologise for the mediocre package name first. This package adds two key functionalities to websockets:
- message handling by type The package adds a cute, easy way to handle messages based on the type of the message Types are fully customizable
- connection keeping and message relay Through message handling it was simple to add message relaying For that we store the incoming websocket connections. (it is also out of the box possible to create virtual servers, called rooms) 2.1. Authentication based on initial http request params is easily customizable
Index ¶
- Constants
- func AuthenticateRoomUserPermitAllowed(rooms RoomControllerI) func(initialParams url.Values) (string, error)
- func AuthenticateUserPermitAll() func(initialParams url.Values) (string, error)
- func AuthenticateUserPermitPassword(correctCombination func(string, string) bool) func(initialParams url.Values) (string, error)
- func ConnectionIDStringToRoomIDAndUserID(connectionID string) (string, string, error)
- func CreateAllowedIdsMapFromSlice(allowedClientIds []string) map[string]bool
- func ExpirationCallbackDateForRepeatingRoom(r *RepeatingRoom) time.Time
- func ExpirationCallbackDateForRepeatingRoomWithUnix(r *RepeatingRoom, nowUnix int64) int64
- func Pmod(a, b int64) int64
- func ReadLines() <-chan string
- func ReadOneLine() (string, error)
- func RoomIDAndUserIDToClientConnectionIDString(roomID, userID string) string
- func StartChatClientLoop(url string, chatMType string) error
- func StartChatClientLoopAs(baseurl, userID string) error
- func StartChatClientLoopForRoom(baseurl, roomID, userID string) error
- func UnmarshalJsonArray(jsonArray string) []string
- func UrlWithParamsForRoomConnection(baseurl, roomID, userID string) string
- func UrlWithParamsForUserConnection(baseurl, userID string) string
- type AuthenticationError
- type BoltRepeatingRoomStorage
- type BoltTemporaryRoomStorage
- func (b BoltTemporaryRoomStorage) CleanExpired(removedCallback func(*TemporaryRoom)) (*TemporaryRoom, error)
- func (b BoltTemporaryRoomStorage) Close() error
- func (b BoltTemporaryRoomStorage) Get(roomID string) RoomI
- func (b BoltTemporaryRoomStorage) Put(roomI RoomI, allowOverride bool) error
- func (b BoltTemporaryRoomStorage) Remove(roomID string) (bool, error)
- type CertAndKeyPaths
- type ClientCloseMessage
- type ClientConnection
- func (c ClientConnection) Close() error
- func (c ClientConnection) ListenLoop(messageHandlers MessageHandlers) (int, string)
- func (c ClientConnection) SendMapTyped(mType string, data map[string]interface{}) error
- func (c ClientConnection) SendRaw(text string) error
- func (c ClientConnection) SendTyped(mType string, data string) error
- type ConnClosedHandlers
- type ConnOpenedHandlers
- type ConnectionMap
- func (m ConnectionMap) AddIfNotConnected(connection ClientConnection) bool
- func (m ConnectionMap) CloseAll() (int, error)
- func (m ConnectionMap) ForAll(f func(connection *ClientConnection))
- func (m ConnectionMap) GetByID(connectionID string) *ClientConnection
- func (m ConnectionMap) IsConnected(connectionID string) bool
- func (m ConnectionMap) IsEmpty() bool
- func (m ConnectionMap) Remove(connectionID string) *ClientConnection
- type EditableRoomController
- func (p *EditableRoomController) AddRoom(roomID string, newRoom RoomI, allowOverride bool) error
- func (p *EditableRoomController) Close() error
- func (p *EditableRoomController) CloseAndRemoveRoom(roomID string) (bool, error)
- func (p *EditableRoomController) ConnectionInRoomClosed(roomID string, userID string) *ClientConnection
- func (p *EditableRoomController) GetRoom(roomID string) RoomI
- func (p *EditableRoomController) Init()
- func (p *EditableRoomController) NewConnectionForRoom(roomID string, connection ClientConnection) bool
- type HTTPRepeatingRoomEditor
- func NewHTTPRepeatingPersistedRoomEditor(bindAddress string, bindPort int, ...) *HTTPRepeatingRoomEditor
- func NewHTTPRepeatingRoomEditor(controller RoomControllerI, bindAddress string, bindPort int, ...) *HTTPRepeatingRoomEditor
- func NewHTTPRepeatingRoomEditorFromCFG(cfg *ini.File) *HTTPRepeatingRoomEditor
- func NewHTTPRepeatingRoomEditorWithStorage(roomStorage RoomStorageI, bindAddress string, bindPort int, ...) *HTTPRepeatingRoomEditor
- func NewHTTPRepeatingRoomPersistingEditorFromCFG(cfg *ini.File) *HTTPRepeatingRoomEditor
- type HTTPRoomEditor
- func NewHTTPRoomEditor(controller RoomControllerI, bindAddress string, bindPort int, ...) *HTTPRoomEditor
- func NewHTTPRoomEditorFromCFG(cfg *ini.File) *HTTPRoomEditor
- func NewHTTPRoomEditorInRam(bindAddress string, bindPort int, ...) *HTTPRoomEditor
- func NewHTTPRoomEditorWithStorage(roomStorage RoomStorageI, bindAddress string, bindPort int, ...) *HTTPRoomEditor
- type HTTPTemporaryRoomEditor
- func NewHTTPTemporaryPersistedRoomEditor(bindAddress string, bindPort int, ...) *HTTPTemporaryRoomEditor
- func NewHTTPTemporaryPersistedRoomEditorFromCfg(cfg *ini.File) *HTTPTemporaryRoomEditor
- func NewHTTPTemporaryRoomEditor(controller RoomControllerI, bindAddress string, bindPort int, ...) *HTTPTemporaryRoomEditor
- func NewHTTPTemporaryRoomEditorFromCFG(cfg *ini.File) *HTTPTemporaryRoomEditor
- func NewHTTPTemporaryRoomEditorInRam(bindAddress string, bindPort int, ...) *HTTPTemporaryRoomEditor
- func NewHTTPTemporaryRoomEditorWithStorage(roomStorage RoomStorageI, bindAddress string, bindPort int, ...) *HTTPTemporaryRoomEditor
- type HttpRouteFunc
- type MessageHandlers
- type MissingURLFieldError
- type PermanentRoom
- type RamRoomStorage
- func (r RamRoomStorage) CleanExpired(removedCallback func(room *TemporaryRoom)) (*TemporaryRoom, error)
- func (r RamRoomStorage) Close() error
- func (r RamRoomStorage) Get(roomID string) RoomI
- func (r RamRoomStorage) Put(room RoomI, allowOverride bool) error
- func (r RamRoomStorage) Remove(roomID string) (bool, error)
- type RepeatingRoom
- type RepeatingRoomController
- func (p *RepeatingRoomController) AddRoom(roomID string, newRoomI RoomI, allowOverride bool) error
- func (p *RepeatingRoomController) Close() error
- func (p *RepeatingRoomController) CloseAndRemoveRoom(roomID string) (bool, error)
- func (p *RepeatingRoomController) NewConnectionForRoom(roomID string, connection ClientConnection) bool
- type Room
- type RoomConnectionsMap
- func (p RoomConnectionsMap) AddConnectionInRoomIfNotConnected(roomID string, connection ClientConnection) bool
- func (p RoomConnectionsMap) CloseAllConnections() (int, error)
- func (p RoomConnectionsMap) CloseAllInRoom(roomID string) (int, error)
- func (p RoomConnectionsMap) ForAllIn(roomID string, f func(connection *ClientConnection))
- func (p *RoomConnectionsMap) ForAllRooms(f func(roomID string))
- func (p RoomConnectionsMap) GetConnectionInRoom(roomID, userID string) *ClientConnection
- func (p RoomConnectionsMap) IsConnected(roomID, userID string) bool
- func (p RoomConnectionsMap) RemoveConnectionInRoom(roomID string, userID string) *ClientConnection
- type RoomControllerI
- type RoomControllers
- func (r *RoomControllers) AddRoom(_ string, _ RoomI, _ bool) error
- func (r *RoomControllers) Close() error
- func (r *RoomControllers) CloseAndRemoveRoom(roomID string) (bool, error)
- func (r *RoomControllers) ConnectionInRoomClosed(roomID string, userID string) *ClientConnection
- func (r *RoomControllers) GetConnectionInRoom(roomID, userID string) *ClientConnection
- func (r *RoomControllers) GetRoom(roomID string) RoomI
- func (r *RoomControllers) Init()
- func (r *RoomControllers) IsConnected(roomID string, userID string) bool
- func (r *RoomControllers) NewConnectionForRoom(roomID string, connection ClientConnection) bool
- type RoomI
- type RoomStorageI
- type Server
- func (s *Server) AddConnClosedHandler(handler func(connectionID string, closeCode int, closeReason string))
- func (s *Server) AddConnOpenedHandler(handler func(ClientConnection))
- func (s *Server) AddDirectForwardingFunctionality(messageTypes ...string)
- func (s *Server) AddMessageHandler(mType string, handler func(string, ClientConnection, map[string]interface{}))
- func (s *Server) AddMessageHandlers(messageHandlers MessageHandlers)
- func (s *Server) AddRoomForwardingFunctionality(roomControllers RoomControllers, messageTypes ...string)
- func (s *Server) AddServerClosedHandler(handler func())
- func (s *Server) Close() error
- func (s *Server) SetAuthenticator(authenticator func(url.Values) (string, error))
- func (s *Server) StartUnencrypted(bindAddress string, bindPort int, httpWsUpgradeRoute string, ...) error
- func (s *Server) StartWithTLS(bindAddress string, bindPort int, httpRoute string, tlsConfig CertAndKeyPaths) error
- func (s *Server) StartWithTLSMultipleCerts(bindAddress string, bindPort int, httpRoute string, ...) error
- type ServerClosedHandlers
- type TemporaryRoom
- type TemporaryRoomController
- type TemporaryRoomStorageI
Constants ¶
const PingInterval = 66
Variables ¶
This section is empty.
Functions ¶
func AuthenticateRoomUserPermitAllowed ¶ added in v1.0.6
func AuthenticateRoomUserPermitAllowed(rooms RoomControllerI) func(initialParams url.Values) (string, error)
Checks for every connection whether the user is not already connected, the room exists and the user is allowed in that room
func AuthenticateUserPermitAll ¶
Default Authenticate Function, which permits all connections that specify a user=<userName> in the params for example: y.x.com/route?user=test.
func AuthenticateUserPermitPassword ¶
func AuthenticateUserPermitPassword(correctCombination func(string, string) bool) func(initialParams url.Values) (string, error)
Default Authenticate Function, which permits all connections that specify a user=<userName> and password=<pw> for example: y.x.com/route?user=test&password=123456bestpw the combination is checked against correctCombination(userName, password), which should check a database or something NOTE: Use ssl. For clear(!) reasons.
func ConnectionIDStringToRoomIDAndUserID ¶ added in v1.0.6
yes, I know the following is ugly, but golang is seriously missing support for generics and this is kinda mostly ok.
func CreateAllowedIdsMapFromSlice ¶ added in v1.0.6
func ExpirationCallbackDateForRepeatingRoom ¶ added in v1.0.6
func ExpirationCallbackDateForRepeatingRoom(r *RepeatingRoom) time.Time
func ExpirationCallbackDateForRepeatingRoomWithUnix ¶ added in v1.0.6
func ExpirationCallbackDateForRepeatingRoomWithUnix(r *RepeatingRoom, nowUnix int64) int64
func ReadOneLine ¶ added in v1.0.6
func RoomIDAndUserIDToClientConnectionIDString ¶ added in v1.0.6
func StartChatClientLoop ¶ added in v1.0.6
Connect to url. Listen to os.stdin for messages to send to remote
func StartChatClientLoopAs ¶ added in v1.0.6
See UrlWithParamsForRoomConnection, StartChatClientLoop
func StartChatClientLoopForRoom ¶ added in v1.0.6
See UrlWithParamsForRoomConnection, StartChatClientLoop
func UnmarshalJsonArray ¶ added in v1.0.6
func UrlWithParamsForRoomConnection ¶ added in v1.0.6
Returns complete url for room connection (baseurl example: http://dns.com:8080/route) Same as http://dns.com:8080/route?user=<userID>&room=<roomID>
func UrlWithParamsForUserConnection ¶ added in v1.0.6
Returns complete url for room connection (baseurl example: http://dns.com:8080/route) Same as http://dns.com:8080/route?user=<userID>
Types ¶
type AuthenticationError ¶
type AuthenticationError struct {
Reason string
}
Error if authentication fails on a connecting websocket
func (AuthenticationError) Error ¶
func (m AuthenticationError) Error() string
type BoltRepeatingRoomStorage ¶ added in v1.0.6
type BoltRepeatingRoomStorage struct {
// contains filtered or unexported fields
}
func NewRepeatingRoomBoltStorage ¶ added in v1.0.6
func NewRepeatingRoomBoltStorage(dbPath string) BoltRepeatingRoomStorage
func (BoltRepeatingRoomStorage) Close ¶ added in v1.0.6
func (b BoltRepeatingRoomStorage) Close() error
func (BoltRepeatingRoomStorage) Get ¶ added in v1.0.6
func (b BoltRepeatingRoomStorage) Get(roomID string) RoomI
type BoltTemporaryRoomStorage ¶ added in v1.0.6
type BoltTemporaryRoomStorage struct {
// contains filtered or unexported fields
}
func NewTemporaryRoomBoltStorage ¶ added in v1.0.6
func NewTemporaryRoomBoltStorage(dbPath string) BoltTemporaryRoomStorage
func (BoltTemporaryRoomStorage) CleanExpired ¶ added in v1.0.6
func (b BoltTemporaryRoomStorage) CleanExpired(removedCallback func(*TemporaryRoom)) (*TemporaryRoom, error)
func (BoltTemporaryRoomStorage) Close ¶ added in v1.0.6
func (b BoltTemporaryRoomStorage) Close() error
func (BoltTemporaryRoomStorage) Get ¶ added in v1.0.6
func (b BoltTemporaryRoomStorage) Get(roomID string) RoomI
type CertAndKeyPaths ¶
func ReadMultipleCertsFromCfg ¶
func ReadMultipleCertsFromCfg(cfg *ini.File) []CertAndKeyPaths
Given cfg requires ssl.<child> sections. Each section must have a 'cert_path' and 'key_path' field. The resulting cert-paths can be used when starting a wsclientable-server
type ClientCloseMessage ¶ added in v1.0.6
type ClientCloseMessage struct {
// contains filtered or unexported fields
}
type ClientConnection ¶
type ClientConnection struct { // stringified type but golang does not(yet) support generics, because 'we don't need it' // (which is why they now add it to the language, just like Java did) // (making the same mistake twice) ID string // contains filtered or unexported fields }
This file gives us the Client Connection type. On a server it represents the connection to a client.
Internally it is a thread safe websocket connection, with an ID. Certain wsclientable modules use that ID to distinguish connections and route messages.
However it can also be used as a connection to the server on the client side.
For that, the 'Connect' constructor can be used.
Apart from thread safety and ID, ClientConnections add only 1 important thing to websockets, Typed messages:
Typed Messages can be sent using 'SendTyped' and 'SendMapTyped'(for json support) Typed Messages can be received over the 'ListenLoop', note that ListenLoop blocks and it can be advisable to run it in a goroutine
func Connect ¶
func Connect(url string) (*ClientConnection, error)
Connect this websocket to the given url (example: http://dns.com:8080/route?user=testUserName) Server at url must be a wsclientable-server for reliable results On handshake problems, check cert (correct domain, still valid, added to local trusted)
func ConnectAs ¶ added in v1.0.6
func ConnectAs(baseurl, userID string) (*ClientConnection, error)
See Connect and UrlWithParamsForRoomConnection
func ConnectToRoom ¶ added in v1.0.6
func ConnectToRoom(baseurl, roomID, userID string) (*ClientConnection, error)
See Connect and UrlWithParamsForRoomConnection
func (ClientConnection) Close ¶
func (c ClientConnection) Close() error
func (ClientConnection) ListenLoop ¶
func (c ClientConnection) ListenLoop(messageHandlers MessageHandlers) (int, string)
enables the listen loop, which will serve the given message handlers. will only return when this connection is closed, so it will typically be run in a goroutine Returns the close code and the closing message (1000 indicates normal closing)
func (ClientConnection) SendMapTyped ¶
func (c ClientConnection) SendMapTyped(mType string, data map[string]interface{}) error
will marshal the given map into json and call SendTyped
func (ClientConnection) SendRaw ¶
func (c ClientConnection) SendRaw(text string) error
type ConnClosedHandlers ¶ added in v1.0.6
type ConnOpenedHandlers ¶ added in v1.0.6
type ConnOpenedHandlers []func(ClientConnection)
type ConnectionMap ¶ added in v1.0.6
type ConnectionMap struct {
// contains filtered or unexported fields
}
This class provides an in-memory, thread safe map from connectionID to ClientConnection. This allows to query a connection by id and send data to it. This is used, for example, to forward message between clients that only know each other by id
func NewConnectionMap ¶ added in v1.0.6
func NewConnectionMap() ConnectionMap
func (ConnectionMap) AddIfNotConnected ¶ added in v1.0.6
func (m ConnectionMap) AddIfNotConnected(connection ClientConnection) bool
adds and returns true when the connection was newly added if this function returns false, the given connection was NOT added to the map and should be closed (id collision)
func (ConnectionMap) CloseAll ¶ added in v1.0.6
func (m ConnectionMap) CloseAll() (int, error)
Iterates the map and closes all connection, returns the latest error (i.e. if there are multiple errors, the method will continue to iterate and return only the latest error)
func (ConnectionMap) ForAll ¶ added in v1.0.6
func (m ConnectionMap) ForAll(f func(connection *ClientConnection))
Calls the given function for all connections in the map
func (ConnectionMap) GetByID ¶ added in v1.0.6
func (m ConnectionMap) GetByID(connectionID string) *ClientConnection
Returns the connection with the id or nil if it does not exist in the map
func (ConnectionMap) IsConnected ¶ added in v1.0.6
func (m ConnectionMap) IsConnected(connectionID string) bool
Whether the map contains a connection with the given id
func (ConnectionMap) IsEmpty ¶ added in v1.0.6
func (m ConnectionMap) IsEmpty() bool
Whether the map is empty
func (ConnectionMap) Remove ¶ added in v1.0.6
func (m ConnectionMap) Remove(connectionID string) *ClientConnection
Removes the connection with the given id from this map Return nil if no connection was removed, otherwise the removed connection is returned (IT IS NOT CLOSED YET)
type EditableRoomController ¶ added in v1.0.6
type EditableRoomController struct { RoomConnectionsMap // contains filtered or unexported fields }
Minimal RoomControllerI implementation
func NewEditableRoomController ¶ added in v1.0.6
func NewEditableRoomController(roomStorage RoomStorageI) EditableRoomController
func NewEditableRoomControllerInRam ¶ added in v1.0.6
func NewEditableRoomControllerInRam() EditableRoomController
func NewPermanentRoomController ¶
func NewPermanentRoomController(rooms ...PermanentRoom) *EditableRoomController
func NewPermanentRoomControllerFromCFG ¶
func NewPermanentRoomControllerFromCFG(cfg *ini.File) *EditableRoomController
func (*EditableRoomController) AddRoom ¶ added in v1.0.6
func (p *EditableRoomController) AddRoom(roomID string, newRoom RoomI, allowOverride bool) error
func (*EditableRoomController) Close ¶ added in v1.0.6
func (p *EditableRoomController) Close() error
func (*EditableRoomController) CloseAndRemoveRoom ¶ added in v1.0.6
func (p *EditableRoomController) CloseAndRemoveRoom(roomID string) (bool, error)
func (*EditableRoomController) ConnectionInRoomClosed ¶ added in v1.0.6
func (p *EditableRoomController) ConnectionInRoomClosed(roomID string, userID string) *ClientConnection
func (*EditableRoomController) GetRoom ¶ added in v1.0.6
func (p *EditableRoomController) GetRoom(roomID string) RoomI
implement interface RoomControllerI:
func (*EditableRoomController) Init ¶ added in v1.0.6
func (p *EditableRoomController) Init()
func (*EditableRoomController) NewConnectionForRoom ¶ added in v1.0.6
func (p *EditableRoomController) NewConnectionForRoom(roomID string, connection ClientConnection) bool
type HTTPRepeatingRoomEditor ¶ added in v1.0.6
type HTTPRepeatingRoomEditor struct {
*HTTPRoomEditor
}
func NewHTTPRepeatingPersistedRoomEditor ¶ added in v1.0.6
func NewHTTPRepeatingPersistedRoomEditor( bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, dbPath string, ) *HTTPRepeatingRoomEditor
func NewHTTPRepeatingRoomEditor ¶ added in v1.0.6
func NewHTTPRepeatingRoomEditor( controller RoomControllerI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPRepeatingRoomEditor
func NewHTTPRepeatingRoomEditorFromCFG ¶ added in v1.0.6
func NewHTTPRepeatingRoomEditorFromCFG(cfg *ini.File) *HTTPRepeatingRoomEditor
func NewHTTPRepeatingRoomEditorWithStorage ¶ added in v1.0.6
func NewHTTPRepeatingRoomEditorWithStorage( roomStorage RoomStorageI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPRepeatingRoomEditor
func NewHTTPRepeatingRoomPersistingEditorFromCFG ¶ added in v1.0.6
func NewHTTPRepeatingRoomPersistingEditorFromCFG(cfg *ini.File) *HTTPRepeatingRoomEditor
func (HTTPRepeatingRoomEditor) Init ¶ added in v1.0.6
func (p HTTPRepeatingRoomEditor) Init()
type HTTPRoomEditor ¶ added in v1.0.6
type HTTPRoomEditor struct { RoomControllerI // contains filtered or unexported fields }
func NewHTTPRoomEditor ¶ added in v1.0.6
func NewHTTPRoomEditor( controller RoomControllerI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPRoomEditor
func NewHTTPRoomEditorFromCFG ¶ added in v1.0.6
func NewHTTPRoomEditorFromCFG(cfg *ini.File) *HTTPRoomEditor
func NewHTTPRoomEditorInRam ¶ added in v1.0.6
func NewHTTPRoomEditorInRam( bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPRoomEditor
func NewHTTPRoomEditorWithStorage ¶ added in v1.0.6
func NewHTTPRoomEditorWithStorage( roomStorage RoomStorageI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPRoomEditor
func (*HTTPRoomEditor) Close ¶ added in v1.0.6
func (p *HTTPRoomEditor) Close() error
implement interface RoomControllerI:
func (*HTTPRoomEditor) Init ¶ added in v1.0.6
func (p *HTTPRoomEditor) Init()
type HTTPTemporaryRoomEditor ¶ added in v1.0.6
type HTTPTemporaryRoomEditor struct {
*HTTPRoomEditor
}
func NewHTTPTemporaryPersistedRoomEditor ¶ added in v1.0.6
func NewHTTPTemporaryPersistedRoomEditor( bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, dbPath string, ) *HTTPTemporaryRoomEditor
func NewHTTPTemporaryPersistedRoomEditorFromCfg ¶ added in v1.0.6
func NewHTTPTemporaryPersistedRoomEditorFromCfg(cfg *ini.File) *HTTPTemporaryRoomEditor
func NewHTTPTemporaryRoomEditor ¶ added in v1.0.6
func NewHTTPTemporaryRoomEditor( controller RoomControllerI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPTemporaryRoomEditor
func NewHTTPTemporaryRoomEditorFromCFG ¶ added in v1.0.6
func NewHTTPTemporaryRoomEditorFromCFG(cfg *ini.File) *HTTPTemporaryRoomEditor
func NewHTTPTemporaryRoomEditorInRam ¶ added in v1.0.6
func NewHTTPTemporaryRoomEditorInRam( bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPTemporaryRoomEditor
func NewHTTPTemporaryRoomEditorWithStorage ¶ added in v1.0.6
func NewHTTPTemporaryRoomEditorWithStorage( roomStorage RoomStorageI, bindAddress string, bindPort int, addRoomRoute, editRoomRoute, removeRoomRoute string, ) *HTTPTemporaryRoomEditor
func (*HTTPTemporaryRoomEditor) Init ¶ added in v1.0.6
func (p *HTTPTemporaryRoomEditor) Init()
type HttpRouteFunc ¶ added in v1.0.6
type HttpRouteFunc struct {
// contains filtered or unexported fields
}
func NewHttpRouteFunc ¶ added in v1.0.6
func NewHttpRouteFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) HttpRouteFunc
type MessageHandlers ¶ added in v1.0.6
type MessageHandlers map[string]func(mType string, client ClientConnection, message map[string]interface{})
type MissingURLFieldError ¶
type MissingURLFieldError struct {
MissingFieldName string
}
error returned if there is a missing field in an http request
func (MissingURLFieldError) Error ¶
func (m MissingURLFieldError) Error() string
type PermanentRoom ¶
type PermanentRoom struct {
Room
}
A PermanentRoom is a Room, that will always return true for RoomI.IsValid
func NewPermanentRoom ¶
func NewPermanentRoom(ID string, allowedClientIds []string) PermanentRoom
Create PermanentRoom that allows only clients with one of the given IDs
func NewPermissiblePermanentRoom ¶
func NewPermissiblePermanentRoom(ID string) PermanentRoom
Create PermanentRoom that allows clients with any ID
func (PermanentRoom) GetID ¶ added in v1.0.6
func (r PermanentRoom) GetID() string
func (PermanentRoom) IsAllowed ¶ added in v1.0.6
func (r PermanentRoom) IsAllowed(userID string) bool
func (PermanentRoom) IsValid ¶ added in v1.0.6
func (r PermanentRoom) IsValid() bool
type RamRoomStorage ¶ added in v1.0.6
type RamRoomStorage struct {
// contains filtered or unexported fields
}
func NewMutableRamRoomStorage ¶ added in v1.0.6
func NewMutableRamRoomStorage() RamRoomStorage
func NewRamRoomStorageFromSlice ¶ added in v1.0.6
func NewRamRoomStorageFromSlice(rooms []PermanentRoom) RamRoomStorage
func (RamRoomStorage) CleanExpired ¶ added in v1.0.6
func (r RamRoomStorage) CleanExpired(removedCallback func(room *TemporaryRoom)) (*TemporaryRoom, error)
func (RamRoomStorage) Close ¶ added in v1.0.6
func (r RamRoomStorage) Close() error
func (RamRoomStorage) Get ¶ added in v1.0.6
func (r RamRoomStorage) Get(roomID string) RoomI
type RepeatingRoom ¶ added in v1.0.6
type RepeatingRoom struct { Room FirstTimeUnixTimestamp int64 RepeatEverySeconds int64 DurationInSeconds int64 }
A repeating room is a room, but only allows clients in the specified time frame
There is NO functionality to close a room when the time is up For that the RoomStorageI.CleanExpired method has to be called - and connections closed via the callback That is left to the controller, because naturally that is the only class with enough information to do so
! However it is very desirable that the RoomStorageI.CleanExpired method has an efficient implementation that scales
func NewRepeatingRoom ¶ added in v1.0.6
func NewRepeatingRoom(ID string, allowedClientIds []string, firstTimeUnixTimestamp, repeatEverySeconds, durationInSeconds int64) RepeatingRoom
Create PermanentRoom that allows only clients with one of the given IDs
func (RepeatingRoom) GetID ¶ added in v1.0.6
func (r RepeatingRoom) GetID() string
func (RepeatingRoom) IsAllowed ¶ added in v1.0.6
func (r RepeatingRoom) IsAllowed(userID string) bool
func (RepeatingRoom) IsValid ¶ added in v1.0.6
func (r RepeatingRoom) IsValid() bool
type RepeatingRoomController ¶ added in v1.0.6
type RepeatingRoomController struct { EditableRoomController // contains filtered or unexported fields }
func NewRepeatingRoomController ¶ added in v1.0.6
func NewRepeatingRoomController(roomStorage RoomStorageI) *RepeatingRoomController
func (*RepeatingRoomController) AddRoom ¶ added in v1.0.6
func (p *RepeatingRoomController) AddRoom(roomID string, newRoomI RoomI, allowOverride bool) error
func (*RepeatingRoomController) Close ¶ added in v1.0.6
func (p *RepeatingRoomController) Close() error
func (*RepeatingRoomController) CloseAndRemoveRoom ¶ added in v1.0.6
func (p *RepeatingRoomController) CloseAndRemoveRoom(roomID string) (bool, error)
func (*RepeatingRoomController) NewConnectionForRoom ¶ added in v1.0.6
func (p *RepeatingRoomController) NewConnectionForRoom(roomID string, connection ClientConnection) bool
type Room ¶
type Room struct { ID string // contains filtered or unexported fields }
Room base struct. Stores its own roomID(ID) Stores all allowedClients, if the list is empty ALL connectionIDs are allowed
type RoomConnectionsMap ¶ added in v1.0.6
type RoomConnectionsMap struct {
// contains filtered or unexported fields
}
This class provides an in-memory, thread safe map from roomID to ConnectionMap. This allows to query connections in rooms (identified by roomID and userID) This is used, for example, to forward message between clients that only know each other by id (but within rooms)
func NewRoomConnectionsMap ¶ added in v1.0.6
func NewRoomConnectionsMap() RoomConnectionsMap
func (RoomConnectionsMap) AddConnectionInRoomIfNotConnected ¶ added in v1.0.6
func (p RoomConnectionsMap) AddConnectionInRoomIfNotConnected(roomID string, connection ClientConnection) bool
Adds the given connection to the given room
func (RoomConnectionsMap) CloseAllConnections ¶ added in v1.0.6
func (p RoomConnectionsMap) CloseAllConnections() (int, error)
Closes all connections in all rooms
func (RoomConnectionsMap) CloseAllInRoom ¶ added in v1.0.6
func (p RoomConnectionsMap) CloseAllInRoom(roomID string) (int, error)
Closes all connections in given room
func (RoomConnectionsMap) ForAllIn ¶ added in v1.0.6
func (p RoomConnectionsMap) ForAllIn(roomID string, f func(connection *ClientConnection))
Calls the given function with all connections in the given room
func (*RoomConnectionsMap) ForAllRooms ¶ added in v1.0.6
func (p *RoomConnectionsMap) ForAllRooms(f func(roomID string))
calls the given function for all registered connections - keep the read lock only while iterating, delete in between is allowed
func (RoomConnectionsMap) GetConnectionInRoom ¶ added in v1.0.6
func (p RoomConnectionsMap) GetConnectionInRoom(roomID, userID string) *ClientConnection
Return the connection under the given roomID, userID combination
func (RoomConnectionsMap) IsConnected ¶ added in v1.0.6
func (p RoomConnectionsMap) IsConnected(roomID, userID string) bool
Whether the given roomID, userID combination can be queried from this map
func (RoomConnectionsMap) RemoveConnectionInRoom ¶ added in v1.0.6
func (p RoomConnectionsMap) RemoveConnectionInRoom(roomID string, userID string) *ClientConnection
Removes the given connection from the given room, removing the mapping entirely Return nil if no connection was removed, otherwise the removed connection is returned (IT IS NOT CLOSED YET)
type RoomControllerI ¶ added in v1.0.6
type RoomControllerI interface { // Initialized the controller, can be expected to have been called before any other methods Init() // Closes all underlying resources. Should be called exactly once. // Should make best effort to close all, even if some intermediate close operations return with an error Close() error // Returns the room under the given id GetRoom(roomID string) RoomI //Closes each connection in the room and removes the room, might allow efficient clean up of associated resources CloseAndRemoveRoom(roomID string) (bool, error) //Add or Edit the given room AddRoom(roomID string, newRoom RoomI, allowOverride bool) error // see RoomConnectionsMap.IsConnected IsConnected(roomID string, userID string) bool // see RoomConnectionsMap.AddConnectionInRoom, except the controller might acquire additional resources NewConnectionForRoom(roomID string, connection ClientConnection) bool // see RoomConnectionsMap.RemoveConnectionInRoom, except the controller might cancel additional resources ConnectionInRoomClosed(roomID string, userID string) *ClientConnection // see RoomConnectionsMap.GetConnectionInRoom GetConnectionInRoom(roomID, userID string) *ClientConnection }
type RoomControllers ¶
type RoomControllers struct {
// contains filtered or unexported fields
}
Bundle of multiple controllers In case of duplicate roomID definitions, the order here determines which room takes precedence
func BundleControllers ¶
func BundleControllers(controllers ...RoomControllerI) RoomControllers
Bundle of multiple controllers In case of duplicate roomID definitions, the order here determines which room takes precedence
func (*RoomControllers) AddRoom ¶ added in v1.0.6
func (r *RoomControllers) AddRoom(_ string, _ RoomI, _ bool) error
func (*RoomControllers) Close ¶ added in v1.0.6
func (r *RoomControllers) Close() error
func (*RoomControllers) CloseAndRemoveRoom ¶ added in v1.0.6
func (r *RoomControllers) CloseAndRemoveRoom(roomID string) (bool, error)
func (*RoomControllers) ConnectionInRoomClosed ¶ added in v1.0.6
func (r *RoomControllers) ConnectionInRoomClosed(roomID string, userID string) *ClientConnection
func (*RoomControllers) GetConnectionInRoom ¶ added in v1.0.6
func (r *RoomControllers) GetConnectionInRoom(roomID, userID string) *ClientConnection
func (*RoomControllers) GetRoom ¶ added in v1.0.6
func (r *RoomControllers) GetRoom(roomID string) RoomI
func (*RoomControllers) Init ¶ added in v1.0.6
func (r *RoomControllers) Init()
func (*RoomControllers) IsConnected ¶ added in v1.0.6
func (r *RoomControllers) IsConnected(roomID string, userID string) bool
func (*RoomControllers) NewConnectionForRoom ¶ added in v1.0.6
func (r *RoomControllers) NewConnectionForRoom(roomID string, connection ClientConnection) bool
type RoomI ¶
type RoomI interface { // The ID of this room GetID() string // Whether the given connectionID is currently allowed in this room. If not, the connection to the client should be closed IsAllowed(userID string) bool // Whether the room is still valid. If this returns false, IsAllowed must return false also IsValid() bool }
Base Interface for all rooms Rooms have an ID Rooms can allow or reject clients (which may depend on the connectionID itself or context factors such as time) Rooms can become Invalid, in which case the would and should be removed by RoomStorageI.CleanExpired
type RoomStorageI ¶ added in v1.0.6
type RoomStorageI interface { // Puts the given room into storage - if already exists: override Put(room RoomI, allowOverride bool) error // Remove room if it exists(return (true, nil), otherwise return (false, nil) // If the underlying storage implementation fails, it may return an error Remove(roomID string) (bool, error) // Returns nil if room does not exist Get(roomID string) RoomI // Closes underlying resources, should only be called once. Has to be called (can be deferred). Close() error }
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewWSHandlingServer ¶
func NewWSHandlingServer() Server
func (*Server) AddConnClosedHandler ¶
func (*Server) AddConnOpenedHandler ¶
func (s *Server) AddConnOpenedHandler(handler func(ClientConnection))
func (*Server) AddDirectForwardingFunctionality ¶
Will add direct relay functionality to the server on the given message types.
for that it will keep a map of currently open connections In the data field it will require a 'to' field, that indicates which connection (with the given name) the message shall be forwarded to the message will be forwarded as is with the original type and the data field exactly as is only a 'from' field will be added/overridden - this from field is verified.
func (*Server) AddMessageHandler ¶
func (s *Server) AddMessageHandler(mType string, handler func(string, ClientConnection, map[string]interface{}))
func (*Server) AddMessageHandlers ¶
func (s *Server) AddMessageHandlers(messageHandlers MessageHandlers)
func (*Server) AddRoomForwardingFunctionality ¶
func (s *Server) AddRoomForwardingFunctionality(roomControllers RoomControllers, messageTypes ...string)
Will add direct relay functionality within rooms (described above)
func (*Server) AddServerClosedHandler ¶
func (s *Server) AddServerClosedHandler(handler func())
func (*Server) SetAuthenticator ¶
func (*Server) StartUnencrypted ¶
func (s *Server) StartUnencrypted(bindAddress string, bindPort int, httpWsUpgradeRoute string, additionalRoutes ...HttpRouteFunc) error
never returns without error - also when locally closed.
Connection will only be http. Some clients(browsers) have opted to disallow unencrypted http connections.
additionalRoutes will be added to server handler by handler.HandleFunc (must not contain conflicting patterns)
func (*Server) StartWithTLS ¶
func (s *Server) StartWithTLS(bindAddress string, bindPort int, httpRoute string, tlsConfig CertAndKeyPaths) error
never returns without error - also when locally closed.
For the certificate to be accepted by the client they must be from a client-local-trusted ca.
func (*Server) StartWithTLSMultipleCerts ¶
func (s *Server) StartWithTLSMultipleCerts(bindAddress string, bindPort int, httpRoute string, tlsConfigs ...CertAndKeyPaths) error
Starts the server with the ssl certificates at the given paths.
For certificates to be accepted by the client they must be from a client-local-trusted ca.
type ServerClosedHandlers ¶ added in v1.0.6
type ServerClosedHandlers []func()
type TemporaryRoom ¶
A temporary room is a room, but only allows clients in the specified time frame
There is NO functionality to close a room when the time is up For that the RoomStorageI.CleanExpired method has to be called - and connections closed via the callback That is left to the controller, because naturally that is the only class with enough information to do so
! However it is very desirable that the RoomStorageI.CleanExpired method has an efficient implementation that scales
func NewTemporaryRoom ¶ added in v1.0.6
func NewTemporaryRoom(ID string, allowedClientIds []string, validFromUnixTime, validUntilUnixTime int64) TemporaryRoom
func (TemporaryRoom) GetID ¶ added in v1.0.6
func (r TemporaryRoom) GetID() string
func (TemporaryRoom) IsAllowed ¶ added in v1.0.6
func (r TemporaryRoom) IsAllowed(userID string) bool
func (TemporaryRoom) IsValid ¶ added in v1.0.6
func (r TemporaryRoom) IsValid() bool
type TemporaryRoomController ¶
type TemporaryRoomController struct { EditableRoomController // contains filtered or unexported fields }
func NewTemporaryRoomController ¶
func NewTemporaryRoomController(roomStorage RoomStorageI) *TemporaryRoomController
func (*TemporaryRoomController) AddRoom ¶ added in v1.0.6
func (p *TemporaryRoomController) AddRoom(roomID string, newRoomI RoomI, allowOverride bool) error
func (*TemporaryRoomController) CloseAndRemoveRoom ¶ added in v1.0.6
func (p *TemporaryRoomController) CloseAndRemoveRoom(roomID string) (bool, error)
type TemporaryRoomStorageI ¶ added in v1.0.6
type TemporaryRoomStorageI interface { RoomStorageI // Cleans all expired rooms // (based on RoomI.IsValid OR some specific logic in case the room storage is specific to a room-type) // Returns the next room that will expire or nil and an error CleanExpired(removedCallback func(*TemporaryRoom)) (*TemporaryRoom, error) }
Source Files ¶
- client.go
- command_line_chat_client.go
- connection_map.go
- defaults.go
- forwarding.go
- http_editor_permanent.go
- http_editor_repeating.go
- http_editor_temporary.go
- mod.go
- room_connections_map.go
- room_controller.go
- room_controller_editable.go
- room_controller_permanent.go
- room_controller_repeating.go
- room_controller_temporary.go
- room_forwarding.go
- room_storage.go
- room_storage_ram.go
- room_storage_repeat_room_db.go
- room_storage_temp_room_db.go
- server.go