-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
140 lines (113 loc) · 3.31 KB
/
Copy pathclient.go
File metadata and controls
140 lines (113 loc) · 3.31 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
//go:build !js
package blazewave
import (
"context"
"net/http"
"time"
"github.com/heyehang/blazewave/core/pool"
"github.com/heyehang/blazewave/core/timer"
)
// Client manages WebSocket event handlers
type Client struct {
// Client options
opts *clientOption
// Global Event
*Event
}
// NewClient creates a new client
func NewClient(options ...ClientOptions) *Client {
opts := &clientOption{
DialOptions: &DialOptions{},
}
for _, option := range options {
option(opts)
}
e := NewEvent()
if opts.DialOptions == nil {
opts.DialOptions = &DialOptions{}
}
opts.DialOptions.Event = e
var c = &Client{
opts: opts,
Event: e,
}
return c
}
type clientOption struct {
*DialOptions
}
type ClientOptions func(*clientOption)
// WithClientHTTPClient sets the HTTP client for the event manager
func WithClientHTTPClient(httpClient *http.Client) ClientOptions {
return func(o *clientOption) {
o.HTTPClient = httpClient
}
}
// WithClientHTTPHeader sets the HTTP header for the event manager
func WithClientHTTPHeader(httpHeader http.Header) ClientOptions {
return func(o *clientOption) {
o.HTTPHeader = httpHeader
}
}
// WithClientHost sets the host for the event manager
func WithClientHost(host string) ClientOptions {
return func(o *clientOption) {
o.Host = host
}
}
// WithClientSubprotocols sets the subprotocols for the event manager
func WithClientSubprotocols(subprotocols []string) ClientOptions {
return func(o *clientOption) {
o.Subprotocols = subprotocols
}
}
// WithClientCompressionMode sets the compression mode for the event manager
func WithClientCompressionMode(compressionMode CompressionMode) ClientOptions {
return func(o *clientOption) {
o.CompressionMode = compressionMode
}
}
// WithClientCompressionThreshold sets the compression threshold for the event manager
func WithClientCompressionThreshold(compressionThreshold int) ClientOptions {
return func(o *clientOption) {
o.CompressionThreshold = compressionThreshold
}
}
// WithClientReaderPool sets the reader pool for the event manager
func WithClientReaderPool(readerPool *pool.Pool) ClientOptions {
return func(o *clientOption) {
o.ReaderPool = readerPool
}
}
// WithClientWriterPool sets the writer pool for the event manager
func WithClientWriterPool(writerPool *pool.Pool) ClientOptions {
return func(o *clientOption) {
o.WriterPool = writerPool
}
}
// WithClientHeartbeatInterval sets the heartbeat interval for the event manager
func WithClientHeartbeatInterval(heartbeatInterval time.Duration) ClientOptions {
return func(o *clientOption) {
o.HeartbeatInterval = heartbeatInterval
}
}
// WithClientHeartbeatTimeout sets the heartbeat timeout for the event manager
func WithClientHeartbeatTimeout(heartbeatTimeout time.Duration) ClientOptions {
return func(o *clientOption) {
o.HeartbeatTimeout = heartbeatTimeout
}
}
// WithClientHeartbeatTimer sets the heartbeat timer for the event manager
func WithClientHeartbeatTimer(heartbeatTimer *timer.Timer) ClientOptions {
return func(o *clientOption) {
o.HeartbeatTimer = heartbeatTimer
}
}
// Dial accepts a WebSocket connection with event-driven capabilities
func (c *Client) Dial(ctx context.Context, url string) (*Conn, *http.Response, error) {
conn, resp, err := Dial(ctx, url, c.opts.DialOptions)
if err != nil {
return nil, resp, err
}
return conn, resp, nil
}