-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
157 lines (128 loc) · 3.92 KB
/
server.go
File metadata and controls
157 lines (128 loc) · 3.92 KB
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
//go:build !js
package blazewave
import (
"net/http"
"time"
"github.com/heyehang/blazewave/core/pool"
"github.com/heyehang/blazewave/core/timer"
)
// Server manages WebSocket event handlers
type Server struct {
// Server options
opts *serverOption
// Global Event
*Event
}
// NewServer creates a new server
func NewServer(options ...ServerOptions) *Server {
opts := &serverOption{
AcceptOptions: &AcceptOptions{},
}
for _, option := range options {
option(opts)
}
e := NewEvent()
if opts.AcceptOptions == nil {
opts.AcceptOptions = &AcceptOptions{}
}
opts.AcceptOptions.Event = e
var s = &Server{
opts: opts,
Event: e,
}
return s
}
type serverOption struct {
*AcceptOptions
}
type ServerOptions func(*serverOption)
// WithServerSubprotocols sets the subprotocols for the event manager
func WithServerSubprotocols(subprotocols []string) ServerOptions {
return func(o *serverOption) {
o.Subprotocols = subprotocols
}
}
// WithServerInsecureSkipVerify sets the insecure skip verify for the event manager
func WithServerInsecureSkipVerify(insecureSkipVerify bool) ServerOptions {
return func(o *serverOption) {
o.InsecureSkipVerify = insecureSkipVerify
}
}
// WithServerCompressionMode sets the compression mode for the event manager
func WithServerCompressionMode(compressionMode CompressionMode) ServerOptions {
return func(o *serverOption) {
o.CompressionMode = compressionMode
}
}
// WithServerCompressionThreshold sets the compression threshold for the event manager
func WithServerCompressionThreshold(compressionThreshold int) ServerOptions {
return func(o *serverOption) {
o.CompressionThreshold = compressionThreshold
}
}
// WithServerOriginPatterns sets the origin patterns for the event manager
func WithServerOriginPatterns(originPatterns []string) ServerOptions {
return func(o *serverOption) {
o.OriginPatterns = originPatterns
}
}
// WithServerReaderPool sets the reader pool for the event manager
func WithServerReaderPool(readerPool *pool.Pool) ServerOptions {
return func(o *serverOption) {
o.ReaderPool = readerPool
}
}
// WithServerWriterPool sets the writer pool for the event manager
func WithServerWriterPool(writerPool *pool.Pool) ServerOptions {
return func(o *serverOption) {
o.WriterPool = writerPool
}
}
// WithServerHeartbeatInterval sets the heartbeat interval for the event manager
func WithServerHeartbeatInterval(heartbeatInterval time.Duration) ServerOptions {
return func(o *serverOption) {
o.HeartbeatInterval = heartbeatInterval
}
}
// WithServerHeartbeatTimeout sets the heartbeat timeout for the event manager
func WithServerHeartbeatTimeout(heartbeatTimeout time.Duration) ServerOptions {
return func(o *serverOption) {
o.HeartbeatTimeout = heartbeatTimeout
}
}
// WithServerHeartbeatTimer sets the heartbeat timer for the event manager
func WithServerHeartbeatTimer(heartbeatTimer *timer.Timer) ServerOptions {
return func(o *serverOption) {
o.HeartbeatTimer = heartbeatTimer
}
}
// Accept accepts a WebSocket connection with event-driven capabilities
func (s *Server) Accept(w http.ResponseWriter, r *http.Request) (*Conn, error) {
conn, err := Accept(w, r, s.opts.AcceptOptions)
if err != nil {
return nil, err
}
return conn, nil
}
// DynamicPools represents pools that can be dynamically assigned per connection
type DynamicPools struct {
ReaderPool pool.BufferPool
WriterPool pool.BufferPool
HeartbeatTimer timer.TimerPool
}
// AcceptWithPools accepts a WebSocket connection with dynamically specified pools
func (s *Server) AcceptWithPools(w http.ResponseWriter, r *http.Request, pools *DynamicPools) (*Conn, error) {
opts := s.opts.AcceptOptions.cloneWithDefaults()
if pools != nil {
if pools.ReaderPool != nil {
opts.ReaderPool = pools.ReaderPool
}
if pools.WriterPool != nil {
opts.WriterPool = pools.WriterPool
}
if pools.HeartbeatTimer != nil {
opts.HeartbeatTimer = pools.HeartbeatTimer
}
}
return accept(w, r, opts)
}