ws

package
v0.0.0-...-23c8eec Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CONNECT = "CONNECT"
	PING    = "PING"
)

Server to client events

View Source
const (
	AUTHENTICATION_NEEDED  = "AUTHENTICATION_NEEDED"
	AUTHENTICATION_SUCCESS = "AUTHENTICATION_SUCCESS"
	AUTHENTICATION_FAIL    = "AUTHENTICATION_FAIL"
	PONG                   = "PONG"
	CLOSE                  = "CLOSE"
	ERROR                  = "ERROR"

	ROOM_CREATE = "ROOM_CREATE"
	ROOM_UPDATE = "ROOM_UPDATE"
	ROOM_DELETE = "ROOM_DELETE"

	MESSAGE_CREATE = "MESSAGE_CREATE"
	MESSAGE_UPDATE = "MESSAGE_UPDATE"
	MESSAGE_DELETE = "MESSAGE_DELETE"

	USER_CREATE     = "USER_CREATE"
	USER_UPDATE     = "USER_UPDATE"
	USER_CONNECT    = "USER_CONNECT"
	USER_DISCONNECT = "USER_DISCONNECT"
	USER_DELETE     = "USER_DELETE"

	GROUP_CREATE = "GROUP_CREATE"
	GROUP_UPDATE = "GROUP_UPDATE"
	GROUP_DELETE = "GROUP_DELETE"
)

Server to Client events

Variables

View Source
var (
	ERROR400 = Error{400, "Unknown client error"}
	ERROR401 = Error{401, "You must authenticate in order to send and receive events"}
	ERROR402 = Error{402, "Invalid event data"}
	ERROR403 = Error{403, "You don't have the permission to send this event"}
	ERROR404 = Error{404, "Unknown event"}
	ERROR405 = Error{405, "A moderator disabled your account. Contact the moderation team to know further details"}

	ERROR500 = Error{400, "Unknown server error"}
)

Error codes sent to the client

View Source
var Pipeline = make(chan Event)

Pipeline is the main channel where events are dispatched to connected users

Functions

func HandlePipeline

func HandlePipeline()

HandlePipeline gets all the events sent in the pipeline and broadcasts them to users

func Ping

func Ping(ws *websocket.Conn, _ *db.User, _ *Event)

Ping is used to check the connection with the server

Types

type Client

type Client struct {
	User          *db.User
	Connection    *websocket.Conn
	Server        *Server
	Pipeline      chan *Event
	ClosePipeline chan bool
}

func NewClient

func NewClient(connection *websocket.Conn, server *Server) *Client

func (*Client) Listen

func (c *Client) Listen()

func (*Client) ListenClientEvents

func (c *Client) ListenClientEvents()

func (*Client) ListenServerEvents

func (c *Client) ListenServerEvents()

type ClientEvent

type ClientEvent func(*websocket.Conn, *db.User, *Event)

ClientEvent is a shortcut for a handler function

type Error

type Error struct {
	// The code of the error (4xx for client errors, 5xx for server errors)
	Code int `json:"code"`
	// The message that explains the error
	Message string `json:"message"`
}

Error represents an error in event processing

type Event

type Event struct {
	// The type of the event, as defined in the constants below
	Event string `json:"event"`
	// Permissions to have in order to receive this event
	Permission Permission `json:"-"`
	// The data related to the event (message, room, user...)
	Data interface{} `json:"data,omitempty"`
}

Event is the contained for all events that'll be sent inside the pipeline

type Permission

type Permission struct {
	// Type of the permission: global - room - any
	Type string
	// ID of the room, if the permission type is room or any
	RoomID uint
	// Permission bit
	Value uint
}

Permission defines the permissions a user must have in order to receive a particular event

type Server

type Server struct {
	ConnectPipeline    chan *Client
	DisconnectPipeline chan *Client
}

func NewServer

func NewServer() *Server

func (*Server) AddClient

func (s *Server) AddClient(client *Client)

func (*Server) DeleteClient

func (s *Server) DeleteClient(client *Client)

func (*Server) Handle

func (s *Server) Handle(connection *websocket.Conn)

func (*Server) Listen

func (s *Server) Listen()

type StationEntity

type StationEntity struct {
	sync.RWMutex
	// contains filtered or unexported fields
}
var Station StationEntity

Station stores connections to the WebSocket server In the future, would be replaced by a concurrent-specific map : https://github.com/streamrail/concurrent-map

func (*StationEntity) EditUser

func (s *StationEntity) EditUser(user *db.User)

func (*StationEntity) GetUser

func (s *StationEntity) GetUser(token string) (user *db.User, found bool)

func (*StationEntity) Initialize

func (s *StationEntity) Initialize()

Jump to

Keyboard shortcuts

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