forked from alexandrevicenzi/go-sse
/
client.go
43 lines (36 loc) · 794 Bytes
/
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
package sse
import (
"github.com/google/uuid"
)
// Client represents a web browser connection.
type Client struct {
uuid string
lastEventID,
channel string
send chan *Message
}
func newClient(lastEventID, channel string) (*Client, error) {
uuid, err := uuid.NewUUID()
if err != nil {
return nil, err
}
return &Client{
uuid.String(),
lastEventID,
channel,
make(chan *Message),
}, nil
}
// SendMessage sends a message to client.
func (c *Client) SendMessage(message *Message) {
c.lastEventID = message.id
c.send <- message
}
// Channel returns the channel where this client is subscribe to.
func (c *Client) Channel() string {
return c.channel
}
// LastEventID returns the ID of the last message sent.
func (c *Client) LastEventID() string {
return c.lastEventID
}