epher

package
v0.0.0-...-fd65962 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Epher

type Epher struct {
	Rooms map[string]Roomer
	// contains filtered or unexported fields
}

Epher is the main struct to store the global state

func New

func New(redis *redis.Client) *Epher

New creates a new global state

func (*Epher) PushHandler

func (e *Epher) PushHandler(rw http.ResponseWriter, r *http.Request)

PushHandler sends the HTTP post to the websocket listeners

func (*Epher) WebsocketHandler

func (e *Epher) WebsocketHandler(rw http.ResponseWriter, r *http.Request)

WebsocketHandler is the public websocket interface For every new websocket connection we'll keep a new handler open

type Room

type Room struct {
	Name string

	Users map[int64]*User
	// contains filtered or unexported fields
}

Room is the global state for each room

func NewRoom

func NewRoom(name string, redisC *redis.Client) *Room

NewRoom creates a new room

func (*Room) AddUser

func (r *Room) AddUser(u *User)

AddUser to room

func (*Room) BroadcastText

func (r *Room) BroadcastText(b []byte) error

BroadcastText broadcast a text message in the room

func (*Room) DelUser

func (r *Room) DelUser(u *User)

DelUser from room

func (*Room) Stop

func (r *Room) Stop()

Stop the pubsub if available

func (*Room) UserCount

func (r *Room) UserCount() int

UserCount returns the user's count

type Roomer

type Roomer interface {
	AddUser(u *User)
	DelUser(u *User)
	UserCount() int
	BroadcastText(b []byte) error
	Stop()
}

Roomer help testing the room related here

type TextSender

type TextSender interface {
	SendText(b []byte) error
}

TextBroadcaster helps mocking the SendText call This is an alternate solution to the Roomer (in the handler.go) which define how a Room should work. Here we just define a single requirement to have the User struct a specific method and tie it to an interface. Now in the room_test.go we could simply overwrite how this function should work

type User

type User struct {
	ID int64

	TextSender // Embed the interface
	// contains filtered or unexported fields
}

User handler to wrap read/write and other data

func NewUser

func NewUser(ws wsConn) *User

NewUser creates a new user

func (*User) ReadLoop

func (u *User) ReadLoop() error

ReadLoop start

func (*User) SendText

func (u *User) SendText(b []byte) error

SendText message to the user

Jump to

Keyboard shortcuts

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