-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient.go
60 lines (49 loc) · 1.31 KB
/
client.go
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
package gosf
import io "github.com/ambelovsky/gosf-socketio"
// Client represents a single connected client
type Client struct {
channel *io.Channel
Rooms []string
}
// Join joins a user to a broadcast room
func (c *Client) Join(room string) {
c.channel.Join(room)
foundRoom := false
for i := range c.Rooms {
if c.Rooms[i] == room {
foundRoom = true
break
}
}
if !foundRoom {
c.Rooms = append(c.Rooms, room)
}
}
// Leave removes a user from a broadcast room
func (c *Client) Leave(room string) {
c.channel.Leave(room)
for i := range c.Rooms {
if c.Rooms[i] == room {
c.Rooms = append(c.Rooms[:i], c.Rooms[i+1:]...)
break
}
}
}
// LeaveAll removes a user from all broadcast rooms they are currently joined to
func (c *Client) LeaveAll() {
for _, v := range c.Rooms {
c.channel.Leave(v)
}
c.Rooms = make([]string, 0)
}
// Disconnect forces a client to be disconnected from the server
func (c *Client) Disconnect() {
c.channel.Close()
}
// Broadcast sends a message to connected clients joined to the same room
// with the exception of the "client"
func (c *Client) Broadcast(room string, endpoint string, message *Message) {
emit("before-client-broadcast", c, room, endpoint, message)
c.channel.BroadcastTo(room, endpoint, message)
emit("after-client-broadcast", c, room, endpoint, message)
}