summaryrefslogtreecommitdiff
path: root/plugins/broadcast/ws/plugin.go
blob: f075864b51bf0875b06410687f4462a936772f44 (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
49
50
51
52
53
54
55
56
57
58
59
package ws

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

const (
	RootPluginName = "broadcast"
	PluginName     = "websockets"
)

type Plugin struct {
	// logger
	log logger.Logger
	// configurer plugin
	cfg config.Configurer
}

func (p *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
	const op = errors.Op("ws_plugin_init")

	// check for the configuration section existence
	if !cfg.Has(RootPluginName) {
		return errors.E(op, errors.Disabled, errors.Str("broadcast plugin section should exists in the configuration"))
	}

	p.cfg = cfg
	p.log = log

	return nil
}

func (p *Plugin) Name() string {
	return PluginName
}

// Provides Provide a ws implementation
func (p *Plugin) Provides() []interface{} {
	return []interface{}{
		p.Websocket,
	}
}

// Websocket method should provide the Subscriber implementation to the broadcast
func (p *Plugin) Websocket(storage broadcast.Storage) (broadcast.Subscriber, error) {
	const op = errors.Op("websocket_subscriber_provide")
	// initialize subscriber with the storage
	ws, err := NewWSSubscriber(storage)
	if err != nil {
		return nil, errors.E(op, err)
	}

	return ws, nil
}

func (p *Plugin) Available() {}