diff options
author | Valery Piashchynski <[email protected]> | 2021-06-16 15:21:56 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-06-16 15:21:56 +0300 |
commit | b1aa5d0ea3617710aec6476bdae956e16b946281 (patch) | |
tree | 5b7c5259375d53b0685bf838555118d5ad93f149 | |
parent | 8220151b6356488b8ef87fa52b14d2178a4e612d (diff) |
- Update ws plugin responses (text instead of binary)
Signed-off-by: Valery Piashchynski <[email protected]>
-rw-r--r-- | plugins/websockets/connection/connection.go | 4 | ||||
-rw-r--r-- | plugins/websockets/executor/executor.go | 7 | ||||
-rw-r--r-- | plugins/websockets/pool/workers_pool.go | 65 | ||||
-rw-r--r-- | tests/plugins/websockets/websocket_plugin_test.go | 16 |
4 files changed, 53 insertions, 39 deletions
diff --git a/plugins/websockets/connection/connection.go b/plugins/websockets/connection/connection.go index 2b847173..04c29d83 100644 --- a/plugins/websockets/connection/connection.go +++ b/plugins/websockets/connection/connection.go @@ -22,7 +22,7 @@ func NewConnection(wsConn *websocket.Conn, log logger.Logger) *Connection { } } -func (c *Connection) Write(mt int, data []byte) error { +func (c *Connection) Write(data []byte) error { c.Lock() defer c.Unlock() @@ -34,7 +34,7 @@ func (c *Connection) Write(mt int, data []byte) error { } }() - err := c.conn.WriteMessage(mt, data) + err := c.conn.WriteMessage(websocket.TextMessage, data) if err != nil { return errors.E(op, err) } diff --git a/plugins/websockets/executor/executor.go b/plugins/websockets/executor/executor.go index e3d47166..5f904d26 100644 --- a/plugins/websockets/executor/executor.go +++ b/plugins/websockets/executor/executor.go @@ -5,7 +5,6 @@ import ( "net/http" "sync" - "github.com/fasthttp/websocket" json "github.com/json-iterator/go" "github.com/spiral/errors" websocketsv1 "github.com/spiral/roadrunner/v2/pkg/proto/websockets/v1beta" @@ -100,7 +99,7 @@ func (e *Executor) StartCommandLoop() error { //nolint:gocognit return errors.E(op, fmt.Errorf("%v,%v", err, errJ)) } - errW := e.conn.Write(websocket.BinaryMessage, packet) + errW := e.conn.Write(packet) if errW != nil { e.log.Error("error writing payload to the connection", "payload", packet, "error", errW) return errors.E(op, fmt.Errorf("%v,%v", err, errW)) @@ -120,7 +119,7 @@ func (e *Executor) StartCommandLoop() error { //nolint:gocognit return errors.E(op, err) } - err = e.conn.Write(websocket.BinaryMessage, packet) + err = e.conn.Write(packet) if err != nil { e.log.Error("error writing payload to the connection", "payload", packet, "error", err) return errors.E(op, err) @@ -150,7 +149,7 @@ func (e *Executor) StartCommandLoop() error { //nolint:gocognit return errors.E(op, err) } - err = e.conn.Write(websocket.BinaryMessage, packet) + err = e.conn.Write(packet) if err != nil { e.log.Error("error writing payload to the connection", "payload", packet, "error", err) return errors.E(op, err) diff --git a/plugins/websockets/pool/workers_pool.go b/plugins/websockets/pool/workers_pool.go index 1a7c6f8a..a196d1f0 100644 --- a/plugins/websockets/pool/workers_pool.go +++ b/plugins/websockets/pool/workers_pool.go @@ -3,11 +3,12 @@ package pool import ( "sync" - "github.com/fasthttp/websocket" + json "github.com/json-iterator/go" websocketsv1 "github.com/spiral/roadrunner/v2/pkg/proto/websockets/v1beta" "github.com/spiral/roadrunner/v2/pkg/pubsub" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/websockets/connection" + "github.com/spiral/roadrunner/v2/utils" ) type WorkersPool struct { @@ -67,6 +68,12 @@ func (wp *WorkersPool) get() map[string]struct{} { return wp.resPool.Get().(map[string]struct{}) } +// Response from the server +type Response struct { + Topic string `json:"topic"` + Payload string `json:"payload"` +} + func (wp *WorkersPool) do() { //nolint:gocognit go func() { for { @@ -89,44 +96,52 @@ func (wp *WorkersPool) do() { //nolint:gocognit continue } - res := wp.get() - + // send a message to every topic for i := 0; i < len(msg.GetTopics()); i++ { + // get free map + res := wp.get() + // get connections for the particular topic br.Connections(msg.GetTopics()[i], res) - } - if len(res) == 0 { - for i := 0; i < len(msg.GetTopics()); i++ { + if len(res) == 0 { wp.log.Info("no such topic", "topic", msg.GetTopics()[i]) + wp.put(res) + continue } - wp.put(res) - continue - } - for i := range res { - c, ok := wp.connections.Load(i) - if !ok { - for i := 0; i < len(msg.GetTopics()); i++ { + // res is a map with a connectionsID + for topic := range res { + c, ok := wp.connections.Load(topic) + if !ok { wp.log.Warn("the user disconnected connection before the message being written to it", "broker", msg.GetBroker(), "topics", msg.GetTopics()[i]) + wp.put(res) + continue } - continue - } - conn := c.(*connection.Connection) + response := &Response{ + Topic: msg.GetTopics()[i], + Payload: utils.AsString(msg.GetPayload()), + } - // put data into the bytes buffer - err := conn.Write(websocket.BinaryMessage, msg.GetPayload()) - if err != nil { - for i := 0; i < len(msg.GetTopics()); i++ { - wp.log.Error("error sending payload over the connection", "error", err, "broker", msg.GetBroker(), "topics", msg.GetTopics()[i]) + d, err := json.Marshal(response) + if err != nil { + wp.log.Error("error marshaling response", "error", err) + wp.put(res) + break + } + + // put data into the bytes buffer + err = c.(*connection.Connection).Write(d) + if err != nil { + for i := 0; i < len(msg.GetTopics()); i++ { + wp.log.Error("error sending payload over the connection", "error", err, "broker", msg.GetBroker(), "topics", msg.GetTopics()[i]) + } + wp.put(res) + continue } - continue } } - - // put map with results back - wp.put(res) case <-wp.exit: wp.log.Info("get exit signal, exiting from the workers pool") return diff --git a/tests/plugins/websockets/websocket_plugin_test.go b/tests/plugins/websockets/websocket_plugin_test.go index 07ee5f12..8321297d 100644 --- a/tests/plugins/websockets/websocket_plugin_test.go +++ b/tests/plugins/websockets/websocket_plugin_test.go @@ -356,7 +356,7 @@ func RPCWsMemoryPubAsync(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP", retMsg) + assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg) // //// LEAVE foo, foo2 ///////// d, err = json.Marshal(messageWS("leave", "memory", []byte("hello websockets"), "foo")) @@ -386,7 +386,7 @@ func RPCWsMemoryPubAsync(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP2", retMsg) + assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg) err = c.WriteControl(websocket.CloseMessage, nil, time.Time{}) assert.NoError(t, err) @@ -430,7 +430,7 @@ func RPCWsMemory(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP", retMsg) + assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg) // //// LEAVE foo, foo2 ///////// d, err = json.Marshal(messageWS("leave", "memory", []byte("hello websockets"), "foo")) @@ -460,7 +460,7 @@ func RPCWsMemory(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP2", retMsg) + assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg) err = c.WriteControl(websocket.CloseMessage, nil, time.Time{}) assert.NoError(t, err) @@ -502,7 +502,7 @@ func RPCWsRedis(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP", retMsg) + assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg) // //// LEAVE foo, foo2 ///////// d, err = json.Marshal(messageWS("leave", "redis", []byte("hello websockets"), "foo")) @@ -532,7 +532,7 @@ func RPCWsRedis(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP2", retMsg) + assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg) err = c.WriteControl(websocket.CloseMessage, nil, time.Time{}) assert.NoError(t, err) @@ -873,7 +873,7 @@ func RPCWsMemoryAllow(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP", retMsg) + assert.Equal(t, "{\"topic\":\"foo\",\"payload\":\"hello, PHP\"}", retMsg) // //// LEAVE foo, foo2 ///////// d, err = json.Marshal(messageWS("leave", "memory", []byte("hello websockets"), "foo")) @@ -903,7 +903,7 @@ func RPCWsMemoryAllow(t *testing.T) { _, msg, err = c.ReadMessage() retMsg = utils.AsString(msg) assert.NoError(t, err) - assert.Equal(t, "hello, PHP2", retMsg) + assert.Equal(t, "{\"topic\":\"foo2\",\"payload\":\"hello, PHP2\"}", retMsg) err = c.WriteControl(websocket.CloseMessage, nil, time.Time{}) assert.NoError(t, err) |