-
Notifications
You must be signed in to change notification settings - Fork 80
/
server.go
161 lines (128 loc) · 3.07 KB
/
server.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package signalr
import (
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-kratos/kratos/v2/encoding"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport"
"github.com/philippseith/signalr"
)
var (
_ transport.Server = (*Server)(nil)
_ transport.Endpointer = (*Server)(nil)
)
type Server struct {
signalr.Server
lis net.Listener
tlsConf *tls.Config
network string
address string
keepAliveInterval time.Duration
chanReceiveTimeout time.Duration
streamBufferCapacity uint
debug bool
err error
codec encoding.Codec
hub signalr.HubInterface
router *http.ServeMux
}
func NewServer(opts ...ServerOption) *Server {
srv := &Server{
network: "tcp",
address: ":0",
router: http.NewServeMux(),
keepAliveInterval: 2 * time.Second,
chanReceiveTimeout: 200 * time.Millisecond,
streamBufferCapacity: 5,
debug: false,
}
srv.init(opts...)
srv.err = srv.listen()
return srv
}
func (s *Server) Name() string {
return string(KindSignalR)
}
func (s *Server) Start(ctx context.Context) error {
if s.err != nil {
return s.err
}
log.Infof("[signalr] server listening on: %s", s.lis.Addr().String())
//handler := handlers.CORS(
// handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
// handlers.AllowedOrigins([]string{"http://localhost:63342"}),
// handlers.AllowedHeaders([]string{"x-requested-with", "x-signalr-user-agent"}),
// handlers.ExposedHeaders([]string{"x-requested-with", "x-signalr-user-agent"}),
// handlers.AllowCredentials(),
//)(s.router)
handler := s.CORS(s.router)
var err error
if s.tlsConf != nil {
err = http.ServeTLS(s.lis, handler, "", "")
} else {
err = http.Serve(s.lis, handler)
}
if !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
func (s *Server) Stop(ctx context.Context) error {
log.Info("[signalr] server stopping")
return s.lis.Close()
}
func (s *Server) Endpoint() (*url.URL, error) {
addr := s.address
prefix := "http://"
if s.tlsConf == nil {
if !strings.HasPrefix(addr, "http://") {
prefix = "http://"
}
} else {
if !strings.HasPrefix(addr, "https://") {
prefix = "https://"
}
}
addr = prefix + addr
var endpoint *url.URL
endpoint, s.err = url.Parse(addr)
return endpoint, nil
}
func (s *Server) MapHTTP(path string) {
s.Server.MapHTTP(signalr.WithHTTPServeMux(s.router), path)
}
func (s *Server) init(opts ...ServerOption) {
for _, o := range opts {
o(s)
}
server, err := signalr.NewServer(context.Background(),
signalr.Logger(&logger{}, s.debug),
//signalr.HubFactory(s.createHub),
signalr.SimpleHubFactory(s.hub),
signalr.KeepAliveInterval(s.keepAliveInterval),
signalr.ChanReceiveTimeout(s.chanReceiveTimeout),
signalr.StreamBufferCapacity(s.streamBufferCapacity),
)
if err != nil {
s.err = err
return
}
s.Server = server
}
func (s *Server) listen() error {
if s.lis == nil {
lis, err := net.Listen(s.network, s.address)
if err != nil {
s.err = err
return err
}
s.lis = lis
}
return nil
}