websocket

package
v0.6.22 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Content managed by Project Forge, see [projectforge.md] for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnMessage

func OnMessage(s *Service, connID uuid.UUID, message *Message) error

Sends a message to a provided Connection ID.

Types

type Channel

type Channel struct {
	Key          string      `json:"key"`
	MemberIDs    []uuid.UUID `json:"memberIDs"`
	LastUpdate   time.Time   `json:"lastUpdate"`
	MessageCount int         `json:"messageCount"`
}

type ConnectEvent

type ConnectEvent func(s *Service, conn *Connection) error

Function used to handle incoming connections.

type Connection

type Connection struct {
	ID       uuid.UUID     `json:"id"`
	Profile  *user.Profile `json:"profile,omitempty"`
	Accounts user.Accounts `json:"accounts,omitempty"`
	Svc      string        `json:"svc,omitempty"`
	ModelID  *uuid.UUID    `json:"modelID,omitempty"`
	Channels []string      `json:"channels,omitempty"`
	// contains filtered or unexported fields
}

Represents a user's WebSocket session.

func NewConnection

func NewConnection(svc string, profile *user.Profile, accounts user.Accounts, socket *websocket.Conn) *Connection

Creates a new Connection.

func (*Connection) Close

func (c *Connection) Close() error

Closes the backing socket.

func (*Connection) Read

func (c *Connection) Read() ([]byte, error)

Reads bytes from this Connection, you should probably use a helper method.

func (*Connection) ToStatus

func (c *Connection) ToStatus() *Status

Transforms this Connection to a serializable Status object.

func (*Connection) Write

func (c *Connection) Write(b []byte) error

Writes bytes to this Connection, you should probably use a helper method.

type Handler

type Handler func(s *Service, conn *Connection, svc string, cmd string, param json.RawMessage) error

Function used to handle incoming messages.

type Message

type Message struct {
	From    *uuid.UUID      `json:"from,omitempty"`
	Channel string          `json:"channel,omitempty"`
	Cmd     string          `json:"cmd"`
	Param   json.RawMessage `json:"param"`
}

Common message struct for passing a service, command and parameter.

func NewMessage

func NewMessage(userID *uuid.UUID, ch string, cmd string, param any) *Message

Constructor.

func (*Message) String

func (m *Message) String() string

Returns a string in "cmd" format, ignoring the param.

type OnlineUpdate

type OnlineUpdate struct {
	UserID    uuid.UUID `json:"userID"`
	Connected bool      `json:"connected"`
}

Message for updates of a user's online status.

type Service

type Service struct {
	Logger util.Logger

	Context any
	// contains filtered or unexported fields
}

Manages all Connection objects.

func NewService

func NewService(logger util.Logger, onOpen ConnectEvent, handler Handler, onClose ConnectEvent, ctx any) *Service

Creates a new service with the provided handler functions.

func (*Service) ChannelList

func (s *Service) ChannelList(params *filter.Params) []string

Returns an array of channels based on the parameters.

func (*Service) Close

func (s *Service) Close()

func (*Service) Count

func (s *Service) Count() int

Total number of all connections managed by this service.

func (*Service) Disconnect

func (s *Service) Disconnect(connID uuid.UUID) (bool, error)

Removes a Connection from this Service.

func (*Service) GetAllMembers added in v0.6.22

func (s *Service) GetAllMembers(key string) []*Connection

func (*Service) GetByID

func (s *Service) GetByID(id uuid.UUID) *Status

Returns a Status representing the Connection with the provided ID.

func (*Service) GetChannel added in v0.6.22

func (s *Service) GetChannel(ch string) *Channel

func (*Service) GetConnection added in v0.6.22

func (s *Service) GetConnection(id uuid.UUID) *Connection

func (*Service) GetOnline

func (s *Service) GetOnline(key string) []uuid.UUID

func (*Service) Join

func (s *Service) Join(connID uuid.UUID, ch string) (bool, error)

func (*Service) Leave

func (s *Service) Leave(connID uuid.UUID, ch string) (bool, error)

func (*Service) OnClose

func (s *Service) OnClose(connID uuid.UUID) error

Callback for when the backing connection is closed.

func (*Service) OnOpen

func (s *Service) OnOpen(connID uuid.UUID) error

Callback for when the backing connection is re-established.

func (*Service) ReadLoop

func (s *Service) ReadLoop(connID uuid.UUID, onDisconnect func(conn *Connection) error) error

func (*Service) Register

func (s *Service) Register(profile *user.Profile, c *websocket.Conn) (*Connection, error)

Registers a new Connection for this Service using the provided fevouser.Profile and websocket.Conn.

func (*Service) Status

func (s *Service) Status() ([]string, []*Connection, any)

func (*Service) UserList

func (s *Service) UserList(params *filter.Params) Statuses

Returns an array of Connection statuses based on the parameters.

func (*Service) Write

func (s *Service) Write(connID uuid.UUID, message string) error

func (*Service) WriteChannel

func (s *Service) WriteChannel(message *Message, except ...uuid.UUID) error

func (*Service) WriteMessage

func (s *Service) WriteMessage(connID uuid.UUID, message *Message) error

type Status

type Status struct {
	ID       uuid.UUID `json:"id"`
	Username string    `json:"username"`
	Channels []string  `json:"channels"`
}

Serializable representation of a Connection.

type Statuses

type Statuses = []*Status

type UpdateMemberParams

type UpdateMemberParams struct {
	ID   uuid.UUID `json:"id"`
	Role string    `json:"role"`
}

Jump to

Keyboard shortcuts

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