forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
50 lines (41 loc) · 890 Bytes
/
helper_test.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
package socketio
import (
"bytes"
"io"
"io/ioutil"
"github.com/googollee/go-engine.io"
)
type WriterNopCloser struct {
io.Writer
}
func NewWriterNopCloser(w io.Writer) io.WriteCloser {
return WriterNopCloser{
Writer: w,
}
}
func (w WriterNopCloser) Close() error {
return nil
}
type FrameData struct {
Buffer *bytes.Buffer
Type engineio.MessageType
}
type FrameSaver struct {
data []FrameData
}
func (f *FrameSaver) NextWriter(t engineio.MessageType) (io.WriteCloser, error) {
data := FrameData{
Buffer: bytes.NewBuffer(nil),
Type: t,
}
f.data = append(f.data, data)
return NewWriterNopCloser(data.Buffer), nil
}
func (f *FrameSaver) NextReader() (engineio.MessageType, io.ReadCloser, error) {
if len(f.data) == 0 {
return engineio.MessageText, nil, io.EOF
}
ret := f.data[0]
f.data = f.data[1:]
return ret.Type, ioutil.NopCloser(ret.Buffer), nil
}