Repository navigation
Expand file tree
/
Copy pathconn_room.go
More file actions
77 lines (65 loc) · 2.16 KB
/
Copy pathconn_room.go
File metadata and controls
77 lines (65 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package neffos
import (
"context"
)
// Room describes a connected connection to a room,
// emits messages with the `Message.Room` filled to the specific room
// and `Message.Namespace` to the underline `NSConn`'s namespace.
type Room struct {
NSConn *NSConn
Name string
}
func newRoom(ns *NSConn, roomName string) *Room {
return &Room{
NSConn: ns,
Name: roomName,
}
}
// String method simply returns the Conn's ID().
// To get the room's name simply use the `Room.Name` struct field instead.
func (r *Room) String() string {
return r.NSConn.String()
}
// excludeKeys implements Sender: passing a Room to `Server.Broadcast` excludes
// the connection that joined it, not the room's members.
func (r *Room) excludeKeys() (serverConnID, connID string) {
return r.NSConn.excludeKeys()
}
// Emit method sends a message to the remote side with its `Message.Room` filled to this specific room
// and `Message.Namespace` to the underline `NSConn`'s namespace.
// It is `Send(event, body) == nil`.
func (r *Room) Emit(event string, body []byte) bool {
return r.Send(event, body) == nil
}
// Send sends a message to the remote side with its `Message.Room` filled to
// this specific room and `Message.Namespace` to the underline `NSConn`'s
// namespace, and returns nil once it was handed off to the socket. A nil Room
// returns ErrBadRoom; see `Conn.Send` for the other errors.
func (r *Room) Send(event string, body []byte) error {
if r == nil {
return ErrBadRoom
}
return r.NSConn.send(Message{
Room: r.Name,
Event: event,
Body: body,
})
}
// SendObject is `Send` for a value: it encodes "v" with `Marshal` and sends the
// result as the body to this room. An encoding error is returned and nothing is sent.
func (r *Room) SendObject(event string, v any) error {
body, err := Marshal(v)
if err != nil {
return err
}
return r.Send(event, body)
}
// Leave method sends a remote and local leave room signal `OnRoomLeave` to this specific room
// and fires the `OnRoomLeft` event if succeed.
func (r *Room) Leave(ctx context.Context) error {
return r.NSConn.askRoomLeave(ctx, Message{
Namespace: r.NSConn.namespace,
Room: r.Name,
Event: OnRoomLeave,
})
}