go-emlid/reach/client/socketio/endpoint.go

39 lines
770 B
Go

package socketio
import (
"net"
"strconv"
"strings"
gosocketio "forge.cadoles.com/Pyxis/golang-socketio"
"github.com/pkg/errors"
)
func EndpointFromHAddr(addr string) (string, error) {
host, rawPort, err := net.SplitHostPort(addr)
if err != nil {
var addrErr *net.AddrError
if !errors.As(err, &addrErr) || !strings.Contains(addrErr.Error(), "missing port in address") {
return "", errors.WithStack(err)
}
host = addr
}
port := int64(80)
if rawPort != "" {
port, err = strconv.ParseInt(rawPort, 10, 32)
if err != nil {
return "", errors.WithStack(err)
}
}
endpoint := Endpoint(host, int(port), false)
return endpoint, nil
}
func Endpoint(host string, port int, secure bool) string {
return gosocketio.GetUrl(host, port, false)
}