summaryrefslogtreecommitdiff
path: root/plugins/websockets/rpc.go
blob: d915aa43b34fbb9aabd26ee6eb817ae92df02551 (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
package websockets

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

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

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

	// just return in case of nil message
	if msg == nil {
		*ok = true
		return nil
	}

	err := r.plugin.Publish(msg)
	if err != nil {
		*ok = false
		return errors.E(op, err)
	}
	*ok = true
	return nil
}

func (r *rpc) PublishAsync(msg []byte, ok *bool) error {
	r.log.Debug("message published", "msg", msg)

	// just return in case of nil message
	if msg == nil {
		*ok = true
		return nil
	}
	// publish to the registered broker
	r.plugin.PublishAsync(msg)

	*ok = true
	return nil
}