72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"path"
|
|
"reflect"
|
|
|
|
"git.loafle.net/commons/logging-go"
|
|
crc "git.loafle.net/commons/rpc-go/client"
|
|
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
|
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"
|
|
)
|
|
|
|
func New(containerType occp.ContainerType, services []interface{}, orderedServices []reflect.Type, portNumber int) (*crc.Client, error) {
|
|
connector, err := newConnector(containerType.String(), portNumber)
|
|
if nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
codec := crpj.NewClientCodec()
|
|
|
|
var rpcRegistry crr.RPCRegistry
|
|
if nil != services && 0 < len(services) {
|
|
rpcRegistry = crr.NewRPCRegistry()
|
|
rpcRegistry.RegisterServices(services...)
|
|
}
|
|
|
|
ch := &ClientHandlers{
|
|
Services: services,
|
|
OrderedServices: orderedServices,
|
|
}
|
|
ch.Name = containerType.String()
|
|
ch.Connector = connector
|
|
ch.RPCCodec = codec
|
|
ch.RPCInvoker = rpcRegistry
|
|
|
|
return nil, &crc.Client{
|
|
ClientHandler: ch,
|
|
}
|
|
}
|
|
|
|
func newConnector(containerType occp.ContainerType, portNumber int) (*csswc.Connectors, error) {
|
|
u := url.URL{
|
|
Scheme: "ws",
|
|
Host: fmt.Sprintf("127.0.0.1:%d", port),
|
|
}
|
|
u.Path = path.Join(u.Path, occc.HTTPEntry_Container)
|
|
|
|
connector := &csswc.Connectors{
|
|
Name: containerType.String(),
|
|
URL: u.String(),
|
|
}
|
|
|
|
connector.ReconnectInterval = 5
|
|
connector.ReconnectTryTime = 2
|
|
connector.MaxMessageSize = 4096
|
|
connector.ReadBufferSize = 4096
|
|
connector.WriteBufferSize = 4096
|
|
connector.PongTimeout = 60
|
|
connector.PingTimeout = 10
|
|
connector.PingPeriod = 9
|
|
|
|
connector.OnDisconnected = func(connector csc.Connector) {
|
|
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
|
|
}
|
|
|
|
return connector, nil
|
|
}
|