server

package
v0.0.0-...-6a36c00 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package server delivers all the functionallity to create a chat server. It supports extensions for multiple transports. See transport.go for more information

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthRegistry

type AuthRegistry struct {
	// contains filtered or unexported fields
}

Stores authenticated users in memory

func NewAuthRegistry

func NewAuthRegistry() *AuthRegistry

func (*AuthRegistry) Authenticate

func (a *AuthRegistry) Authenticate(userId string, pass string) bool

Authenticates authenticates the user id with it's password

func (*AuthRegistry) Register

func (a *AuthRegistry) Register(username string, pass string) (string, bool)

Register generates server domain user id which for now is current ts in nanoseconds

func (*AuthRegistry) SearchContact

func (a *AuthRegistry) SearchContact(username string) string

SearchContact searches globally for contact with param username

type Config

type Config struct {
	ListenHost       string
	FileServerHost   string
	FileServerPath   string
	FileServerFolder string
	SSLKeyPath       string
	SSLCertPath      string
}

func NewConfig

func NewConfig() *Config

type ContactStore

type ContactStore struct {
	// contains filtered or unexported fields
}

Stores contact lists for users

func NewContactStore

func NewContactStore() *ContactStore

func (*ContactStore) AddContact

func (c *ContactStore) AddContact(userId string, contactId string) bool

AddContact adds contact in user's contact list

func (*ContactStore) ListContacts

func (c *ContactStore) ListContacts(userId string) ([]string, bool)

ListContacts returns user's contact list for userId

func (*ContactStore) RemoveContact

func (c *ContactStore) RemoveContact(userId string, contactId string) bool

RemoveContact removes contact from user's contact list

type Control

type Control struct {
	// contains filtered or unexported fields
}

Main facility which handles all the control protocol messages

func NewControl

func NewControl(input chan *common.Message,
	outputStore *OutputStore, contactStore *ContactStore,
	authRegistry *AuthRegistry) *Control

func (Control) AddContact

func (c Control) AddContact(user string, contactId string) bool

AddContact handles protocol "add_contact"

func (Control) AddToGroup

func (c Control) AddToGroup(groupId string, userId string) bool

AddToGroup handles protocol "add_to_group"

func (*Control) CreateGroup

func (c *Control) CreateGroup() string

CreateGroup handles protocol "create_group"

func (*Control) DeleteGroup

func (c *Control) DeleteGroup(groupId string) bool

DeleteGroup handles protocol "delete_group"

func (Control) ListContacts

func (c Control) ListContacts(user string) ([]string, bool)

ListContacts handles protocol "list_contacts" Returns array with contact ids

func (Control) ListGroups

func (c Control) ListGroups(userId string) *[]string

ListGroups handles protocol "list_groups"

func (Control) RemoveContact

func (c Control) RemoveContact(user string, contactId string) bool

RemoveContact handles protocol "remove_contact"

func (Control) RemoveFromGroup

func (c Control) RemoveFromGroup(groupId string, userId string) bool

RemoveToGroup handles protocol "remove_from_group"

func (Control) SearchContact

func (c Control) SearchContact(user string, contactName string) string

SearchContact handles protocol "search_contact" Used for finding a user by username

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

Stores the files locally and expose http interfaces from where you can download the files by file token

func NewFileStore

func NewFileStore(folder, host, networkPath string) *FileStore

func (FileStore) AddTextFile

func (store FileStore) AddTextFile(fileContent string) string

AddTextFile stores the stores the file in root folder and generates access uri

func (FileStore) RemoveFile

func (store FileStore) RemoveFile(uri string) bool

RemoveFile removes the file for this uri

type Group

type Group struct {
	Id  string
	Out chan *common.Message
	// contains filtered or unexported fields
}

Aggregate output. When a message is received in the Out channel it will send it to all outputs in the group. Like a multicast channel

func NewGroup

func NewGroup() *Group

func (*Group) AddOutput

func (g *Group) AddOutput(id string, output chan<- *common.Message) bool

AddOutput adds output in the aggregate

func (*Group) RemoveOutput

func (g *Group) RemoveOutput(id string) bool

RemoveOutput removes output from the aggregate

type History

type History struct {
	// contains filtered or unexported fields
}

Stores message history

func NewHistory

func NewHistory(input chan *common.Message, outputStore *OutputStore) *History

func (History) AddMessage

func (h History) AddMessage(msg *common.Message) bool

AddMessage adds in the history

func (History) GetHistory

func (h History) GetHistory(userId string, forUserId string,
	from time.Time, to time.Time) []*common.Message

GetHistory returns a history for user conversation for a time period

type Messanger

type Messanger struct {
	// contains filtered or unexported fields
}

Handler for all messages/files communication between the clients.

func NewMessanger

func NewMessanger(input chan *common.Message,
	history *History, outputStore *OutputStore, fileStore *FileStore) *Messanger

NewMessenger creates a new messagener object which handlers messages through input channel.

type OutputStore

type OutputStore struct {
	// contains filtered or unexported fields
}

Store for all authenticated outputs. Main facility to send messages to remote users

func NewOutputStore

func NewOutputStore() *OutputStore

NewOutputStore returns a valid store.

func (*OutputStore) AddOutput

func (store *OutputStore) AddOutput(id string,
	output chan<- *common.Message) error

AddOutput adds user output in the store by id

func (OutputStore) GetOutput

func (store OutputStore) GetOutput(id string) chan<- *common.Message

GetOutput returns a user output from the store by id

func (*OutputStore) RemoveOutput

func (store *OutputStore) RemoveOutput(id string) error

RemoveOutput removes user output from the store by id

func (OutputStore) Send

func (store OutputStore) Send(msg *common.Message)

Send sends a message to user's output

type Server

type Server struct {
	// contains filtered or unexported fields
}

Main server type

func New

func New(config *Config) *Server

New creates new server instance

func (*Server) AddTransport

func (s *Server) AddTransport(host string, t Transport) error

AddTransport adds transport which later will listen on host

func (*Server) RemoveTransport

func (s *Server) RemoveTransport(host string) error

RemoveTransport removes the transport for the host

func (*Server) Start

func (s *Server) Start(done <-chan interface{}) error

Start starts each transport in own gorouting. Blocks until done channel is not closed

func (*Server) StartAsync

func (s *Server) StartAsync() (chan interface{}, error)

StartAsync is same as Start by runs in async mode

Example
s := New(getConfig())
wss := NewWebSocket(getConfig())
s.AddTransport(":9999", &wss)
done, err := s.StartAsync()
if err != nil {
	log.Println(err)
	return
}

go func() {
	time.Sleep(time.Second * 3)
	close(done)
}()
<-done
Output:

type Transport

type Transport interface {
	OnUserConnected(chan<- *common.User)
	OnUserDisconnected(chan<- *common.User)
	Start(host string, done <-chan interface{})
}

Defines the interface for the transport implementations

type UserHandler

type UserHandler func(*common.User)

type WebSocketServer

type WebSocketServer struct {
	// contains filtered or unexported fields
}

Transport implementation with websocket protocol

func NewWebSocket

func NewWebSocket(config *Config) WebSocketServer

func (*WebSocketServer) OnUserConnected

func (wss *WebSocketServer) OnUserConnected(handler chan<- *common.User)

func (*WebSocketServer) OnUserDisconnected

func (wss *WebSocketServer) OnUserDisconnected(handler chan<- *common.User)

func (*WebSocketServer) ServeHTTP

func (wss *WebSocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*WebSocketServer) Start

func (wss *WebSocketServer) Start(host string, done <-chan interface{})

Jump to

Keyboard shortcuts

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