diff --git a/README.md b/README.md index 078331c..a31626f 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@
-
-
+
+
@@ -51,7 +51,7 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}
diff --git a/client.go b/client.go
index 5722db2..b3af43d 100644
--- a/client.go
+++ b/client.go
@@ -22,7 +22,12 @@ type Client interface {
// NewClient creates a new client to connect to broker server.
func NewClient(uri string) (Client, error) {
- conn, err := net.Dial("tcp", uri)
+ url, err := urlUnpack(uri)
+ if err != nil {
+ return nil, fmt.Errorf("invalid uri: %w", err)
+ }
+
+ conn, err := net.Dial("tcp", url.address)
if err != nil {
return nil, fmt.Errorf("failed to connect to server: %w", err)
}
diff --git a/example/client/multi-subscribe/main.go b/example/client/multi-subscribe/main.go
index 1c081e4..b3721de 100644
--- a/example/client/multi-subscribe/main.go
+++ b/example/client/multi-subscribe/main.go
@@ -8,14 +8,14 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}
for i := 0; i < 2; i++ {
go func(j int) {
- cli, er := stallion.NewClient("localhost:9090")
+ cli, er := stallion.NewClient("st://localhost:9090")
if er != nil {
log.Fatal(er)
}
diff --git a/example/client/simple-publish/main.go b/example/client/simple-publish/main.go
index 98295be..3960cfe 100644
--- a/example/client/simple-publish/main.go
+++ b/example/client/simple-publish/main.go
@@ -8,7 +8,7 @@ import (
)
func main() {
- client, err := stallion.NewClient("localhost:9090")
+ client, err := stallion.NewClient("st://localhost:9090")
if err != nil {
panic(err)
}
diff --git a/url.go b/url.go
new file mode 100644
index 0000000..a4341d9
--- /dev/null
+++ b/url.go
@@ -0,0 +1,38 @@
+package stallion
+
+import (
+ "fmt"
+ "strings"
+)
+
+// url
+// each url contains of the following parts:
+// - host
+// - port
+type url struct {
+ address string
+}
+
+// urlUnpack
+// manages to create url struct from url string.
+func urlUnpack(inputUrl string) (*url, error) {
+ // check the input url protocol
+ protocolSplit := strings.Split(inputUrl, "://")
+ if len(protocolSplit) < 2 {
+ return nil, fmt.Errorf("invalid uri")
+ }
+
+ // protocol must be 'st'
+ if protocolSplit[0] != "st" {
+ return nil, fmt.Errorf("not using stallion protocol (st://...)")
+ }
+
+ // exporting the host:port pair.
+ if len(strings.Split(protocolSplit[1], ":")) < 2 {
+ return nil, fmt.Errorf("server ip or port is not given")
+ }
+
+ return &url{
+ address: protocolSplit[1],
+ }, nil
+}