Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
DefaultMarshal = json.Marshal // default marshal function, you can change it
)
Functions ¶
This section is empty.
Types ¶
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) PushMessage ¶
type ConnWS ¶
type ConnWS struct {
// contains filtered or unexported fields
}
ConnWS Websocket connection
func (*ConnWS) PushMessage ¶
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 ¶
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 ¶
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 ¶
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
type NatsRoom ¶
type NatsRoom[T IMessage] struct { // contains filtered or unexported fields }
NatsRoom is a room by nats implement IRoom interface
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 ¶
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