container-go/client/client.go

51 lines
1.4 KiB
Go
Raw Normal View History

2018-05-02 10:59:45 +00:00
package client
import (
"fmt"
2018-05-03 06:19:55 +00:00
"net/http"
2018-05-02 10:59:45 +00:00
"net/url"
"path"
"git.loafle.net/commons/logging-go"
2018-05-03 06:22:10 +00:00
csc "git.loafle.net/commons/server-go/client"
2018-05-02 10:59:45 +00:00
csswc "git.loafle.net/commons/server-go/socket/web/client"
occc "git.loafle.net/overflow/commons-go/config/container"
occp "git.loafle.net/overflow/commons-go/config/probe"
)
2018-05-03 06:14:22 +00:00
func NewConnector(containerType occp.ContainerType, portNumber int) (*csswc.Connectors, error) {
2018-05-02 10:59:45 +00:00
u := url.URL{
Scheme: "ws",
2018-05-03 06:15:43 +00:00
Host: fmt.Sprintf("127.0.0.1:%d", portNumber),
2018-05-02 10:59:45 +00:00
}
u.Path = path.Join(u.Path, occc.HTTPEntry_Container)
connector := &csswc.Connectors{
2018-05-03 06:19:55 +00:00
URL: u.String(),
2018-05-02 10:59:45 +00:00
}
2018-05-03 06:19:55 +00:00
connector.Name = containerType.String()
2018-05-02 10:59:45 +00:00
connector.ReconnectInterval = 5
connector.ReconnectTryTime = 2
connector.MaxMessageSize = 4096
connector.ReadBufferSize = 4096
connector.WriteBufferSize = 4096
connector.PongTimeout = 60
connector.PingTimeout = 10
connector.PingPeriod = 9
2018-05-03 06:19:55 +00:00
connector.RequestHeader = func() http.Header {
header := make(map[string][]string)
header[occc.HTTPRequestHeaderKey_Container_Method] = []string{occc.HTTPRequestHeaderValue_Container_Method_Connect}
2018-05-03 06:20:55 +00:00
header[occc.HTTPRequestHeaderKey_Container_Type] = []string{containerType.String()}
2018-05-03 06:19:55 +00:00
return header
}
connector.ResponseHandler = func(res *http.Response) {
}
2018-05-02 10:59:45 +00:00
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
return connector, nil
}