-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
53 lines (41 loc) · 903 Bytes
/
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
package mattress
import (
"bytes"
"encoding/json"
"github.com/giskook/gotcp"
"log"
)
type Conn struct {
conn *gotcp.Conn
recieveBuffer *bytes.Buffer
mattressid uint64
}
func NewConn(conn *gotcp.Conn) *Conn {
return &Conn{
conn: conn,
recieveBuffer: bytes.NewBuffer([]byte{}),
}
}
func (c *Conn) Close() {
c.recieveBuffer.Reset()
}
func (c *Conn) GetBuffer() *bytes.Buffer {
return c.recieveBuffer
}
type Callback struct{}
func (this *Callback) OnConnect(c *gotcp.Conn) bool {
conn := NewConn(c)
c.PutExtraData(conn)
return true
}
func (this *Callback) OnClose(c *gotcp.Conn) {
conn := c.GetExtraData().(*Conn)
conn.Close()
}
func (this *Callback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
packet := p.(*ReportStatusPacket)
res, _ := json.Marshal(packet)
GetMqttClient().Publish("hello", string(res))
log.Println(string(res))
return true
}