Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit 6305346

Browse files
authored
chore:unify option (#11)
* chore:unify option * docs:fix a description * docs:replace validator with callback * chore:change client struct
1 parent 6514a9e commit 6305346

File tree

5 files changed

+107
-52
lines changed

5 files changed

+107
-52
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ func main() {
8989
c := sse.NewClient("http://127.0.0.1:8888/sse")
9090

9191
// touch off when connected to the server
92-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
93-
hlog.Infof("client1 connect to server %s success with %s method", c.URL, c.Method)
92+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
93+
hlog.Infof("client1 connect to server %s success with %s method", c.GetURL(), c.GetMethod())
9494
})
9595

9696
// touch off when the connection is shutdown
97-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
98-
hlog.Infof("client1 disconnect to server %s success with %s method", c.URL, c.Method)
97+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
98+
hlog.Infof("client1 disconnect to server %s success with %s method", c.GetURL(), c.GetMethod())
9999
})
100100

101101
events := make(chan *sse.Event)
@@ -126,13 +126,13 @@ func main() {
126126
c := sse.NewClient("http://127.0.0.1:8888/sse")
127127

128128
// touch off when connected to the server
129-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
130-
hlog.Infof("client2 %s connect to server success with %s method", c.URL, c.Method)
129+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
130+
hlog.Infof("client2 %s connect to server success with %s method", c.GetURL(), c.GetMethod())
131131
})
132132

133133
// touch off when the connection is shutdown
134-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
135-
hlog.Infof("client2 %s disconnect to server success with %s method", c.URL, c.Method)
134+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
135+
hlog.Infof("client2 %s disconnect to server success with %s method", c.GetURL(), c.GetMethod())
136136
})
137137

138138
events := make(chan *sse.Event)

README_CN.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ func main() {
8787
c := sse.NewClient("http://127.0.0.1:8888/sse")
8888

8989
// 连接到服务端的时候触发
90-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
91-
hlog.Infof("client1 connect to server %s success with %s method", c.URL, c.Method)
90+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
91+
hlog.Infof("client1 connect to server %s success with %s method", c.GetURL(), c.GetMethod())
9292
})
9393

9494
// 服务端断开连接的时候触发
95-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
96-
hlog.Infof("client1 disconnect to server %s success with %s method", c.URL, c.Method)
95+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
96+
hlog.Infof("client1 disconnect to server %s success with %s method", c.GetURL(), c.GetMethod())
9797
})
9898

9999
events := make(chan *sse.Event)
@@ -124,13 +124,13 @@ func main() {
124124
c := sse.NewClient("http://127.0.0.1:8888/sse")
125125

126126
// 连接到服务端的时候触发
127-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
128-
hlog.Infof("client2 %s connect to server success with %s method", c.URL, c.Method)
127+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
128+
hlog.Infof("client2 %s connect to server success with %s method",c.GetURL(), c.GetMethod())
129129
})
130130

131131
// 服务端断开连接的时候触发
132-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
133-
hlog.Infof("client2 %s disconnect to server success with %s method", c.URL, c.Method)
132+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
133+
hlog.Infof("client2 %s disconnect to server success with %s method", c.GetURL(), c.GetMethod())
134134
})
135135

136136
events := make(chan *sse.Event)

client.go

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,34 @@ var (
4242
// ConnCallback defines a function to be called on a particular connection event
4343
type ConnCallback func(ctx context.Context, client *Client)
4444

45-
// ResponseValidator validates a response
46-
type ResponseValidator func(ctx context.Context, req *protocol.Request, resp *protocol.Response) error
45+
// ResponseCallback validates a response
46+
type ResponseCallback func(ctx context.Context, req *protocol.Request, resp *protocol.Response) error
4747

4848
// Client handles an incoming server stream
4949
type Client struct {
50-
HertzClient *client.Client
50+
hertzClient *client.Client
5151
disconnectCallback ConnCallback
5252
connectedCallback ConnCallback
53-
ResponseValidator ResponseValidator
54-
Headers map[string]string
55-
URL string
56-
Method string
53+
responseCallback ResponseCallback
54+
headers map[string]string
55+
url string
56+
method string
5757
maxBufferSize int
5858
connected bool
59-
EncodingBase64 bool
60-
LastEventID atomic.Value // []byte
59+
encodingBase64 bool
60+
lastEventID atomic.Value // []byte
6161
}
6262

6363
var defaultClient, _ = client.NewClient(client.WithDialer(standard.NewDialer()), client.WithResponseBodyStream(true))
6464

6565
// NewClient creates a new client
6666
func NewClient(url string) *Client {
6767
c := &Client{
68-
URL: url,
69-
HertzClient: defaultClient,
70-
Headers: make(map[string]string),
68+
url: url,
69+
hertzClient: defaultClient,
70+
headers: make(map[string]string),
7171
maxBufferSize: 1 << 16,
72-
Method: consts.MethodGet,
72+
method: consts.MethodGet,
7373
}
7474

7575
return c
@@ -91,8 +91,8 @@ func (c *Client) SubscribeWithContext(ctx context.Context, handler func(msg *Eve
9191
protocol.ReleaseRequest(req)
9292
protocol.ReleaseResponse(resp)
9393
}()
94-
if validator := c.ResponseValidator; validator != nil {
95-
err = validator(ctx, req, resp)
94+
if Callback := c.responseCallback; Callback != nil {
95+
err = Callback(ctx, req, resp)
9696
if err != nil {
9797
return err
9898
}
@@ -147,9 +147,9 @@ func (c *Client) readLoop(ctx context.Context, reader *EventStreamReader, outCh
147147
var msg *Event
148148
if msg, err = c.processEvent(event); err == nil {
149149
if len(msg.ID) > 0 {
150-
c.LastEventID.Store(msg.ID)
150+
c.lastEventID.Store(msg.ID)
151151
} else {
152-
msg.ID, _ = c.LastEventID.Load().(string)
152+
msg.ID, _ = c.lastEventID.Load().(string)
153153
}
154154

155155
// Send downstream if the event has something useful
@@ -160,13 +160,13 @@ func (c *Client) readLoop(ctx context.Context, reader *EventStreamReader, outCh
160160
}
161161
}
162162

163-
// OnDisconnect specifies the function to run when the connection disconnects
164-
func (c *Client) OnDisconnect(fn ConnCallback) {
163+
// SetDisconnectCallback specifies the function to run when the connection disconnects
164+
func (c *Client) SetDisconnectCallback(fn ConnCallback) {
165165
c.disconnectCallback = fn
166166
}
167167

168-
// OnConnect specifies the function to run when the connection is successful
169-
func (c *Client) OnConnect(fn ConnCallback) {
168+
// SetOnConnectCallback specifies the function to run when the connection is successful
169+
func (c *Client) SetOnConnectCallback(fn ConnCallback) {
170170
c.connectedCallback = fn
171171
}
172172

@@ -175,24 +175,79 @@ func (c *Client) SetMaxBufferSize(size int) {
175175
c.maxBufferSize = size
176176
}
177177

178+
// SetURL set sse client url
179+
func (c *Client) SetURL(url string) {
180+
c.url = url
181+
}
182+
183+
// SetMethod set sse client request method
184+
func (c *Client) SetMethod(method string) {
185+
c.method = method
186+
}
187+
188+
// SetHeaders set sse client headers
189+
func (c *Client) SetHeaders(headers map[string]string) {
190+
c.headers = headers
191+
}
192+
193+
// SetResponseCallback set sse client responseCallback
194+
func (c *Client) SetResponseCallback(responseCallback ResponseCallback) {
195+
c.responseCallback = responseCallback
196+
}
197+
198+
// SetHertzClient set sse client
199+
func (c *Client) SetHertzClient(hertzClient *client.Client) {
200+
c.hertzClient = hertzClient
201+
}
202+
203+
// SetEncodingBase64 set sse client whether use the base64
204+
func (c *Client) SetEncodingBase64(encodingBase64 bool) {
205+
c.encodingBase64 = encodingBase64
206+
}
207+
208+
// GetURL get sse client url
209+
func (c *Client) GetURL() string {
210+
return c.url
211+
}
212+
213+
// GetHeaders get sse client headers
214+
func (c *Client) GetHeaders() map[string]string {
215+
return c.headers
216+
}
217+
218+
// GetMethod get sse client method
219+
func (c *Client) GetMethod() string {
220+
return c.method
221+
}
222+
223+
// GetHertzClient get sse client
224+
func (c *Client) GetHertzClient() *client.Client {
225+
return c.hertzClient
226+
}
227+
228+
// GetLastEventID get sse client lastEventID
229+
func (c *Client) GetLastEventID() []byte {
230+
return c.lastEventID.Load().([]byte)
231+
}
232+
178233
func (c *Client) request(ctx context.Context, req *protocol.Request, resp *protocol.Response) error {
179-
req.SetMethod(c.Method)
180-
req.SetRequestURI(c.URL)
234+
req.SetMethod(c.method)
235+
req.SetRequestURI(c.url)
181236

182237
req.Header.Set("Cache-Control", "no-cache")
183238
req.Header.Set("Accept", "text/event-stream")
184239
req.Header.Set("Connection", "keep-alive")
185240

186-
lastID, exists := c.LastEventID.Load().([]byte)
241+
lastID, exists := c.lastEventID.Load().([]byte)
187242
if exists && lastID != nil {
188243
req.Header.Set(LastEventID, string(lastID))
189244
}
190245
// Add user specified headers
191-
for k, v := range c.Headers {
246+
for k, v := range c.headers {
192247
req.Header.Set(k, v)
193248
}
194249

195-
err := c.HertzClient.Do(ctx, req, resp)
250+
err := c.hertzClient.Do(ctx, req, resp)
196251
return err
197252
}
198253

@@ -227,7 +282,7 @@ func (c *Client) processEvent(msg []byte) (event *Event, err error) {
227282
// Trim the last "\n" per the spec.
228283
e.Data = bytes.TrimSuffix(e.Data, []byte("\n"))
229284

230-
if c.EncodingBase64 {
285+
if c.encodingBase64 {
231286
buf := make([]byte, base64.StdEncoding.DecodedLen(len(e.Data)))
232287

233288
n, err := base64.StdEncoding.Decode(buf, e.Data)

client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestClientOnConnect(t *testing.T) {
205205
c := NewClient("http://127.0.0.1:9000/sse")
206206

207207
called := make(chan struct{})
208-
c.OnConnect(func(ctx context.Context, client *Client) {
208+
c.SetOnConnectCallback(func(ctx context.Context, client *Client) {
209209
called <- struct{}{}
210210
})
211211

examples/client/quickstart/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func main() {
5353
c := sse.NewClient("http://127.0.0.1:8888/sse")
5454

5555
// touch off when connected to the server
56-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
57-
hlog.Infof("client1 connect to server %s success with %s method", c.URL, c.Method)
56+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
57+
hlog.Infof("client1 connect to server %s success with %s method", c.GetURL(), c.GetMethod())
5858
})
5959

6060
// touch off when the connection is shutdown
61-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
62-
hlog.Infof("client1 disconnect to server %s success with %s method", c.URL, c.Method)
61+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
62+
hlog.Infof("client1 disconnect to server %s success with %s method", c.GetURL(), c.GetMethod())
6363
})
6464

6565
events := make(chan *sse.Event)
@@ -89,13 +89,13 @@ func main() {
8989
c := sse.NewClient("http://127.0.0.1:8888/sse")
9090

9191
// touch off when connected to the server
92-
c.OnConnect(func(ctx context.Context, client *sse.Client) {
93-
hlog.Infof("client2 %s connect to server success with %s method", c.URL, c.Method)
92+
c.SetOnConnectCallback(func(ctx context.Context, client *sse.Client) {
93+
hlog.Infof("client2 %s connect to server success with %s method", c.GetURL(), c.GetMethod())
9494
})
9595

9696
// touch off when the connection is shutdown
97-
c.OnDisconnect(func(ctx context.Context, client *sse.Client) {
98-
hlog.Infof("client2 %s disconnect to server success with %s method", c.URL, c.Method)
97+
c.SetDisconnectCallback(func(ctx context.Context, client *sse.Client) {
98+
hlog.Infof("client2 %s disconnect to server success with %s method", c.GetURL(), c.GetMethod())
9999
})
100100

101101
events := make(chan *sse.Event)

0 commit comments

Comments
 (0)