forked from umirode/echo-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
144 lines (126 loc) · 3.32 KB
/
wrapper.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package echo_socket_io
import (
"net/http"
"github.com/admpub/log"
socketio "github.com/googollee/go-socket.io"
"github.com/googollee/go-socket.io/engineio"
"github.com/webx-top/echo"
)
// Socket.io wrapper interface
type IWrapper interface {
OnConnect(nsp string, f func(echo.Context, socketio.Conn) error)
OnDisconnect(nsp string, f func(echo.Context, socketio.Conn, string))
OnError(nsp string, f func(echo.Context, socketio.Conn, error))
OnEvent(nsp, event string, f func(echo.Context, socketio.Conn, string))
Serve()
Close()
echo.Handler
}
var _ IWrapper = (*Wrapper)(nil)
func ConnInitor(r *http.Request, c engineio.Conn) {
c.SetContext(getContextByStd(r.Context()))
}
type Wrapper struct {
Server *socketio.Server
}
// Create wrapper and Socket.io server
func NewWrapper(options *engineio.Options) *Wrapper {
if options == nil {
options = &engineio.Options{
ConnInitor: ConnInitor,
}
} else {
if options.ConnInitor == nil {
options.ConnInitor = ConnInitor
} else {
customInitor := options.ConnInitor
options.ConnInitor = func(r *http.Request, c engineio.Conn) {
ConnInitor(r, c)
customInitor(r, c)
}
}
}
server := socketio.NewServer(options)
return &Wrapper{
Server: server,
}
}
// Create wrapper with exists Socket.io server
func NewWrapperWithServer(server *socketio.Server) (*Wrapper, error) {
if server == nil {
return nil, ErrServerCannotBeNil
}
return &Wrapper{
Server: server,
}, nil
}
// On Socket.io client connect
func (s *Wrapper) OnConnect(nsp string, f func(echo.Context, socketio.Conn) error) {
s.Server.OnConnect(nsp, func(conn socketio.Conn) error {
ctx := getContext(conn)
if ctx == nil {
return ErrContextCannotBeNil
}
return f(ctx, conn)
})
}
// On Socket.io client disconnect
func (s *Wrapper) OnDisconnect(nsp string, f func(echo.Context, socketio.Conn, string)) {
s.Server.OnDisconnect(nsp, func(conn socketio.Conn, msg string) {
ctx := getContext(conn)
if ctx == nil {
return
}
f(ctx, conn, msg)
})
}
// On Socket.io error
func (s *Wrapper) OnError(nsp string, f func(echo.Context, socketio.Conn, error)) {
s.Server.OnError(nsp, func(conn socketio.Conn, err error) {
ctx := getContext(conn)
if ctx == nil {
log.Error(err)
return
}
f(ctx, conn, err)
})
}
// On Socket.io event from client
func (s *Wrapper) OnEvent(nsp, event string, f func(echo.Context, socketio.Conn, string)) {
s.Server.OnEvent(nsp, event, func(conn socketio.Conn, msg string) {
ctx := getContext(conn)
if ctx == nil {
return
}
f(ctx, conn, msg)
})
}
// On Socket.io event from client
func (s *Wrapper) OnEventAndReturn(nsp, event string, f func(echo.Context, socketio.Conn, string) string) {
s.Server.OnEvent(nsp, event, func(conn socketio.Conn, msg string) string {
ctx := getContext(conn)
if ctx == nil {
return ErrContextCannotBeNil.Error()
}
return f(ctx, conn, msg)
})
}
// Handler function
func (s *Wrapper) Handle(context echo.Context) error {
s.Server.ServeHTTP(context.Response().StdResponseWriter(), context.Request().StdRequest().WithContext(echo.AsStdContext(context)))
return nil
}
func (s *Wrapper) Serve() {
go func() {
err := s.Server.Serve()
if err != nil {
log.Errorf(`socket.io: %s`, err.Error())
}
}()
}
func (s *Wrapper) Close() {
err := s.Server.Close()
if err != nil {
log.Errorf(`socket.io: %s`, err.Error())
}
}