From 88a60751829014274324767c1cbf4ec40a67a6d1 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 22 Dec 2017 11:50:43 +0300 Subject: [PATCH] [Fixes #23] Fix bug when non-JSON messages broadcasted as nulls --- .gitignore | 2 ++ CHANGELOG.md | 6 ++++++ hub.go | 5 +++++ hub_test.go | 10 ++++++++++ server.go | 6 +++--- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 96a3eccd..76287c29 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ pkg/ tmp/ coverage/ dist/ + +debug* \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 083d248b..d0c4efca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change log +## 0.5.2 (2017-12-22) + +- Fix bug with non-JSON messages. ([@palkan][]) + +Fixes [#23](https://github.com/anycable/anycable-go/issues/23). + ## 0.5.1 (2017-11-08) - Add TLS support. ([@palkan][]) diff --git a/hub.go b/hub.go index 08ffd54e..747adbb9 100644 --- a/hub.go +++ b/hub.go @@ -188,5 +188,10 @@ func BuildMessage(data string, identifier string) []byte { json.Unmarshal([]byte(data), &msg) + // Handle non-JSON payloads as plain strings + if msg == nil { + return (&Reply{Identifier: identifier, Message: data}).toJSON() + } + return (&Reply{Identifier: identifier, Message: msg}).toJSON() } diff --git a/hub_test.go b/hub_test.go index 8cac1e6e..ee886417 100644 --- a/hub_test.go +++ b/hub_test.go @@ -48,3 +48,13 @@ func TestUnsubscribeRaceConditions(t *testing.T) { assert.Equal(t, 0, hub.Size(), "Connections size must be equal 0") } + +func TestBuildMessageJSON(t *testing.T) { + expected := []byte("{\"identifier\":\"chat\",\"message\":{\"text\":\"hello!\"}}") + assert.Equal(t, expected, BuildMessage("{\"text\":\"hello!\"}", "chat")) +} + +func TestBuildMessageString(t *testing.T) { + expected := []byte("{\"identifier\":\"chat\",\"message\":\"plain string\"}") + assert.Equal(t, expected, BuildMessage("plain string", "chat")) +} diff --git a/server.go b/server.go index 5a4a733b..80eb286d 100644 --- a/server.go +++ b/server.go @@ -53,7 +53,7 @@ type Config struct { var ( config = &Config{} - version = "0.5.1" + version = "0.5.2" log = logging.MustGetLogger("main") @@ -64,8 +64,8 @@ var ( wspath = flag.String("wspath", "/cable", "WS endpoint path") disconnectRate = flag.Int("disconnect_rate", 100, "the number of Disconnect calls per second") headers_list = flag.String("headers", "cookie", "list of headers to proxy to RPC") - sslCert = flag.String("ssl_cert", "", "SSL certificate path") - sslKey = flag.String("ssl_key", "", "SSL private key path") + sslCert = flag.String("ssl_cert", "", "SSL certificate path") + sslKey = flag.String("ssl_key", "", "SSL private key path") upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true },