@@ -42,34 +42,34 @@ var (
4242// ConnCallback defines a function to be called on a particular connection event
4343type 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
4949type 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
6363var defaultClient , _ = client .NewClient (client .WithDialer (standard .NewDialer ()), client .WithResponseBodyStream (true ))
6464
6565// NewClient creates a new client
6666func 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+
178233func (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 )
0 commit comments