-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
52 lines (46 loc) · 1.26 KB
/
write.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
package stompy
import (
"io"
"strconv"
)
type stompWriter interface {
io.Writer
WriteByte(c byte) error
Flush() error
}
func writeFrame(writer stompWriter, frame Frame, encode encoder) error {
//set content length
frame.Headers["content-length"] = strconv.Itoa(len(frame.Body))
//write our command CONNECT SUBSCRIBE etc to the buffer
_, err := writer.Write(frame.Command)
if err != nil {
//treating failure to write to the socket as a network error
return err
}
//write each of our headers to the buffer
for k, v := range frame.Headers {
val := encode.Encode(k) + ":" + encode.Encode(v) + "\n"
if _, err := writer.Write([]byte(val)); err != nil {
return err
}
}
//as per the spec add a new line after the header to the buffer
if err := writer.WriteByte('\n'); err != nil {
return err
}
//write our body if there is one to the buffer
if len(frame.Body) > 0 {
if _, err := writer.Write(frame.Body); err != nil {
return err
}
}
//stomp protocol want a null byte at the end of the frame
if err := writer.WriteByte('\x00'); err != nil {
return err
}
//again when we flush the buffer to the socket then if there is an error it is a network error
if err := writer.Flush(); err != nil {
return ConnectionError(err.Error())
}
return nil
}