Skip to content

Commit

Permalink
[Fixes #23] Fix bug when non-JSON messages broadcasted as nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Dec 22, 2017
1 parent 893965b commit 88a6075
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ pkg/
tmp/
coverage/
dist/

debug*
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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][])
Expand Down
5 changes: 5 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
10 changes: 10 additions & 0 deletions hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Config struct {
var (
config = &Config{}

version = "0.5.1"
version = "0.5.2"

log = logging.MustGetLogger("main")

Expand All @@ -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 },
Expand Down

0 comments on commit 88a6075

Please sign in to comment.