summaryrefslogtreecommitdiff
path: root/plugins/websockets/rpc.go
blob: f917bd531e3575753ea50e2de2bced053b11c841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package websockets

import (
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pubsub"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

// rpc collectors struct
type rpc struct {
	plugin *Plugin
	log    logger.Logger
}

func (r *rpc) Publish(msg []*pubsub.Msg, ok *bool) error {
	const op = errors.Op("broadcast_publish")
	r.log.Debug("message published", "msg", msg)

	// publish to the registered broker
	mi := make([]pubsub.Message, 0, len(msg))
	// golang can't convert slice in-place
	// so, we need to convert it manually
	for i := 0; i < len(msg); i++ {
		mi = append(mi, msg[i])
	}
	err := r.plugin.Publish(mi)
	if err != nil {
		*ok = false
		return errors.E(op, err)
	}
	*ok = true
	return nil
}

func (r *rpc) PublishAsync(msg []*pubsub.Msg, ok *bool) error {
	// publish to the registered broker
	mi := make([]pubsub.Message, 0, len(msg))
	// golang can't convert slice in-place
	// so, we need to convert it manually
	for i := 0; i < len(msg); i++ {
		mi = append(mi, msg[i])
	}

	r.plugin.PublishAsync(mi)

	*ok = true
	return nil
}