room

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

room

Easy to expand message distribution component, can be used for chat rooms, and has achieved horizontal expansion of clusters with long connections to ws or sse using nats go

Example

a sample chat room, you can run it directly

start nats

docker run -d -p 4222:4222 nats:latest

start chat room

cd ./examples/chat-room/ && go build && ./chat-room

open browser

http://localhost:8080/index

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultMarshal = json.Marshal // default marshal function, you can change it
)

Functions

This section is empty.

Types

type AMessage

type AMessage []interface{}

AMessage is a []interface{} message

func (AMessage) Bytes

func (r AMessage) Bytes() ([]byte, error)

type ConnSSE

type ConnSSE struct {
	http.ResponseWriter
	http.Flusher
}

ConnSSE Server-Sent Events connection

func NewConnSSE

func NewConnSSE(responseWriter http.ResponseWriter, flusher http.Flusher) *ConnSSE

func (*ConnSSE) Close

func (r *ConnSSE) Close() error

func (*ConnSSE) Heartbeat

func (r *ConnSSE) Heartbeat() error

func (*ConnSSE) PushMessage

func (r *ConnSSE) PushMessage(message IMessage) error

type ConnWS

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

ConnWS Websocket connection

func NewConnWS

func NewConnWS(conn *websocket.Conn) *ConnWS

func (*ConnWS) Close

func (r *ConnWS) Close() error

func (*ConnWS) Heartbeat

func (r *ConnWS) Heartbeat() error

func (*ConnWS) PushMessage

func (r *ConnWS) PushMessage(message IMessage) error

type HMessage

type HMessage map[string]interface{}

HMessage is a map[string]interface{} message

func (HMessage) Bytes

func (r HMessage) Bytes() ([]byte, error)

type IConn

type IConn interface {

	// Heartbeat Send heartbeat to client
	Heartbeat() error

	// PushMessage Send message to client
	PushMessage(message IMessage) error

	// Close Connection will be closed
	Close() error
}

IConn Client long connection interface, already implemented with ws(ConnWS) and sse(ConnSSE)

type IMessage

type IMessage interface {
	Bytes() ([]byte, error)
}

IMessage is a message interface You can implement this interface to define your own message type Broadcast will call Bytes() to get the message bytes

type IRoom

type IRoom interface {

	// ID returns room id
	ID() string

	// Init initializes room
	Init() error

	// Enter adds user to room
	Enter(user IUser) error

	// Leave removes user from room
	Leave(user IUser) error

	// Broadcast broadcasts message to all users in room
	Broadcast(data IMessage) error

	// Close closes room
	Close() error
}

IRoom is a room interface

func NewNatsRoom

func NewNatsRoom[T IMessage](id string, natConn *nats.Conn, opt ...RoomOption) IRoom

type IRooms

type IRooms interface {

	// Init initializes rooms
	Init() error

	// Room returns room by id
	Room(id string) (IRoom, error)

	// OpenRoom opens a room
	OpenRoom(id string) (IRoom, error)

	// CloseRoom closes a room
	CloseRoom(id string) error

	// Close closes rooms
	Close() error
}

IRooms is a rooms interface You can implement this interface to define your own rooms type It represents a collection of all rooms, and you can manage the rooms through it

type IUser

type IUser interface {
	ID() string
	Close() error
	Conn() IConn
}

IUser is a user interface You can implement this interface to define your own user type It represents a user in a room

type MemoryRoom

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

MemoryRoom is a room in memory implement IRoom interface

func NewMemoryRoom

func NewMemoryRoom(id string, opt ...RoomOption) *MemoryRoom

func (*MemoryRoom) Broadcast

func (r *MemoryRoom) Broadcast(data IMessage) error

func (*MemoryRoom) Close

func (r *MemoryRoom) Close() error

func (*MemoryRoom) Enter

func (r *MemoryRoom) Enter(user IUser) error

func (*MemoryRoom) ID

func (r *MemoryRoom) ID() string

func (*MemoryRoom) Init

func (r *MemoryRoom) Init() error

func (*MemoryRoom) Leave

func (r *MemoryRoom) Leave(user IUser) error

type MemoryRooms

type MemoryRooms struct {
	NewRoom func(id string) IRoom
	// contains filtered or unexported fields
}

MemoryRooms is a rooms in memory implement IRooms interface You can use room id to route connections with the same room number to the same node, such as using nginx

func NewMemoryRooms

func NewMemoryRooms(newRoom func(id string) IRoom) *MemoryRooms

func (*MemoryRooms) Close

func (r *MemoryRooms) Close() error

func (*MemoryRooms) CloseRoom

func (r *MemoryRooms) CloseRoom(id string) error

func (*MemoryRooms) Init

func (r *MemoryRooms) Init() error

func (*MemoryRooms) OpenRoom

func (r *MemoryRooms) OpenRoom(id string) (IRoom, error)

func (*MemoryRooms) Room

func (r *MemoryRooms) Room(id string) (IRoom, error)

type NatsRoom

type NatsRoom[T IMessage] struct {
	// contains filtered or unexported fields
}

NatsRoom is a room by nats implement IRoom interface

func (*NatsRoom[T]) Broadcast

func (r *NatsRoom[T]) Broadcast(data IMessage) error

func (*NatsRoom[T]) Close

func (r *NatsRoom[T]) Close() error

func (*NatsRoom[T]) Enter

func (r *NatsRoom[T]) Enter(user IUser) error

func (*NatsRoom[T]) ID

func (r *NatsRoom[T]) ID() string

func (*NatsRoom[T]) Init

func (r *NatsRoom[T]) Init() (err error)

func (*NatsRoom[T]) Leave

func (r *NatsRoom[T]) Leave(user IUser) error

type NatsRooms

type NatsRooms struct {
	NewRoom func(id string, conn *nats.Conn) IRoom
	// contains filtered or unexported fields
}

NatsRooms is a rooms by nats implement IRooms interface nats is a message queue, so we don't need to use channel to send message

func NewNatsRooms

func NewNatsRooms(natsConn *nats.Conn, newRoom func(id string, conn *nats.Conn) IRoom) *NatsRooms

func (*NatsRooms) Close

func (r *NatsRooms) Close() error

func (*NatsRooms) CloseRoom

func (r *NatsRooms) CloseRoom(id string) error

func (*NatsRooms) Init

func (r *NatsRooms) Init() (err error)

func (*NatsRooms) OpenRoom

func (r *NatsRooms) OpenRoom(id string) (IRoom, error)

func (*NatsRooms) Room

func (r *NatsRooms) Room(id string) (IRoom, error)

type RoomOption added in v1.0.1

type RoomOption func(opts *RoomOptions)

func SetEnterBuffSize added in v1.0.1

func SetEnterBuffSize(buffSize int) RoomOption

func SetLeaveBuffSize added in v1.0.1

func SetLeaveBuffSize(buffSize int) RoomOption

func SetMsgBuffSize added in v1.0.1

func SetMsgBuffSize(buffSize int) RoomOption

func SetOnUserEnter added in v1.0.1

func SetOnUserEnter(onUserEnter func(room IRoom, user IUser)) RoomOption

func SetOnUserLeave added in v1.0.1

func SetOnUserLeave(onUserLeave func(room IRoom, user IUser)) RoomOption

type RoomOptions added in v1.0.1

type RoomOptions struct {
	MsgBuffSize   int
	EnterBuffSize int
	LeaveBuffSize int
	OnUserEnter   func(room IRoom, user IUser)
	OnUserLeave   func(room IRoom, user IUser)
}

func DefaultRoomOptions added in v1.0.1

func DefaultRoomOptions() *RoomOptions

type User

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

User is a user implementation of IUser

func NewUser

func NewUser(id string, conn IConn) *User

func (*User) Close

func (r *User) Close() error

func (*User) Conn

func (r *User) Conn() IConn

func (*User) ID

func (r *User) ID() string

Jump to

Keyboard shortcuts

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