Skip to content

Commit 52023d6

Browse files
authored
sign websocket request (#68)
support customize signRequestFunc for sc client
1 parent 29f8e09 commit 52023d6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

client.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ type Client struct {
8585
pool *addresspool.Pool
8686
}
8787

88+
func (c *Client) dialWebsocket(url *url.URL) (*websocket.Conn, *http.Response, error) {
89+
var err error
90+
handshakeReq := &http.Request{Header: c.GetDefaultHeaders(), URL: url}
91+
if c.opt.SignRequest != nil {
92+
if err = c.opt.SignRequest(handshakeReq); err != nil {
93+
openlog.Error("sign websocket request failed" + err.Error())
94+
return nil, nil, err
95+
}
96+
} else if httpclient.SignRequest != nil {
97+
if err = httpclient.SignRequest(handshakeReq); err != nil {
98+
openlog.Error("sign websocket request failed" + err.Error())
99+
return nil, nil, err
100+
}
101+
}
102+
103+
return c.wsDialer.Dial(url.String(), handshakeReq.Header)
104+
}
105+
88106
type PeerStatusResp struct {
89107
Peers []*Peer `json:"peers"`
90108
}
@@ -819,7 +837,7 @@ func (c *Client) setupWSConnection(microServiceID, microServiceInstanceID string
819837
InstancePath, microServiceInstanceID, "/heartbeat"),
820838
}
821839

822-
conn, _, err := c.wsDialer.Dial(u.String(), c.GetDefaultHeaders())
840+
conn, _, err := c.dialWebsocket(&u)
823841
if err != nil {
824842
openlog.Error(fmt.Sprintf("watching microservice dial catch an exception,microServiceID: %s, error:%s", microServiceID, err.Error()))
825843
return err
@@ -996,7 +1014,7 @@ func (c *Client) WatchMicroServiceWithExtraHandle(microServiceID string, callbac
9961014
Path: fmt.Sprintf("%s%s/%s%s", MSAPIPath,
9971015
MicroservicePath, microServiceID, WatchPath),
9981016
}
999-
conn, _, err := c.wsDialer.Dial(u.String(), c.GetDefaultHeaders())
1017+
conn, _, err := c.dialWebsocket(&u)
10001018
if err != nil {
10011019
c.watchers[microServiceID] = false
10021020
c.mutex.Unlock()
@@ -1098,7 +1116,7 @@ func (c *Client) WatchMicroService(microServiceID string, callback func(*MicroSe
10981116
Path: fmt.Sprintf("%s%s/%s%s", MSAPIPath,
10991117
MicroservicePath, microServiceID, WatchPath),
11001118
}
1101-
conn, _, err := c.wsDialer.Dial(u.String(), c.GetDefaultHeaders())
1119+
conn, _, err := c.dialWebsocket(&u)
11021120
if err != nil {
11031121
c.watchers[microServiceID] = false
11041122
c.mutex.Unlock()

0 commit comments

Comments
 (0)