-
Notifications
You must be signed in to change notification settings - Fork 6
/
conn.go
100 lines (84 loc) · 2.73 KB
/
conn.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package sse
import (
"encoding/json"
"encoding/xml"
)
type Conn struct {
messages chan message
shutdown chan bool
isOpen bool
}
type message struct {
message []byte
typ string
id string
}
// Sends a byte-slice to the connected client. Returns an error
// if the connection is already closed.
func (c *Conn) Write(msg []byte) error {
return c.WriteEvent("", msg)
}
// Sends a byte-slice to the connected client and triggers the specified event with the data. Returns an error
// if the connection is already closed.
func (c *Conn) WriteEvent(typ string, msg []byte) error {
return c.WriteEventWithID("", typ, msg)
}
// Sends a byte-slice to the connected client and triggers the specified event with the data. Returns an error
// if the connection is already closed.
func (c *Conn) WriteEventWithID(id, typ string, msg []byte) error {
if !c.isOpen {
return ErrConnectionClosed
}
c.messages <- message{
message: msg,
typ: typ,
id: id,
}
return nil
}
// Sends a string to the connected client. Returns an error
// if the connection is already closed.
func (c *Conn) WriteString(msg string) error {
return c.WriteEvent("", []byte(msg))
}
// Sends a string to the connected client, targeting the specified event. Returns an error
// if the connection is already closed.
func (c *Conn) WriteStringEvent(typ, msg string) error {
return c.WriteEvent(typ, []byte(msg))
}
// Sends a json-encoded struct to the connected client. Returns an error
// if the connection is already closed or if the encoding failed.
func (c *Conn) WriteJson(value interface{}) error {
return c.WriteJsonEvent("", value)
}
// Sends a json-encoded struct to the connected client, targeting the specified event. Returns an error
// if the connection is already closed or if the encoding failed.
func (c *Conn) WriteJsonEvent(typ string, value interface{}) error {
if by, err := json.Marshal(value); err == nil {
return c.WriteEvent(typ, by)
} else {
return err
}
}
// Sends a xml-encoded struct to the connected client. Returns an error
// if the connection is already closed or if the encoding failed.
func (c *Conn) WriteXml(value interface{}) error {
return c.WriteXmlEvent("", value)
}
// Sends a xml-encoded struct to the connected client, targeting the specified event. Returns an error
// if the connection is already closed or if the encoding failed.
func (c *Conn) WriteXmlEvent(typ string, value interface{}) error {
if by, err := xml.Marshal(value); err == nil {
return c.WriteEvent(typ, by)
} else {
return err
}
}
// Returns whether the connection is still opened.
func (c *Conn) IsOpen() bool {
return c.isOpen
}
// Forces the connection to close. The Conn object should not be used anymore.
func (c *Conn) Close() {
c.shutdown <- true
}