actioncable

package module
v0.0.0-...-00ce6b9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 14 Imported by: 0

README

Build Status

Actioncable-Client-Go

Actioncable Client Library for Go.

Requirements

Go v1.10 or later.

Usage

Connections

u, _ := url.Parse("ws://example.org")
consumer := actioncable.CreateConsumer(u, nil)
consumer.Connect()

You can also customize the HTTP header by using NewConsumerOptions.

u, _ := url.Parse("ws://example.org")

header := http.Header{}
header.Set("User-Agent", "Dummy")

opt := actioncable.NewConsumerOptions()
opt.SetHeader(&header)

consumer := actioncable.CreateConsumer(u, opt)
consumer.Connect()

Subscriptions

params := map[string]interface{}{
    "room":"Best Room"
}

id := actioncable.NewChannelIdentifier("ChatChannel", params)
subscription := consumer.Subscriptions.Create(id)
subscription.SetHandler(&ChatSubscriptionEventHandler{})


type ChatSubscriptionEventHandler {
    actioncable.SubscriptionEventHandler
}

func (h *ChatSubscriptionEventHandler) OnConnected(se *actioncable.SubscriptionEvent) {
    fmt.Println("on connected")
}

func (h *ChatSubscriptionEventHandler) OnDisconnected(se *actioncable.SubscriptionEvent) {
    fmt.Println("on disconnected")
}

func (h *ChatSubscriptionEventHandler) OnRejected(se *actioncable.SubscriptionEvent) {
    fmt.Println("on rejected")
}

func (h *ChatSubscriptionEventHandler) OnReceived(se *actioncable.SubscriptionEvent) {
    data := map[string]interface{}{}
    se.ReadJSON(&data)
    fmt.Println(data)
}

You can send data from the client side to the server side. For example:

// id := actioncable.NewChannelIdentifier("ChatChannel", params)
// subscription := consumer.Subscriptions.Create(id)

func (h *ChatSubscriptionEventHandler) OnConnected(se *actioncable.SubscriptionEvent) {
    data := map[string]interface{}{
        "fieldA":"valueA",
        "fieldB":"valueB",
    }

    // # Calls `ChatChannel#appear(data)` on the server.
    subscription.Peform("appear", data)
}

License

MIT License

Test

go test -v ./...

Integration Test with ActionCable Server

(cd test_rails_server; ./bin/setup; bundle exec rails -p 3000 -d) # start actioncable server
TEST_WS="ws://localhost:3000/cable" go test -v ./...
kill $(cat ./test_rails_server/tmp/pids/server.pid) # stop actioncable server

Documentation

Index

Constants

View Source
const (
	// use the same value as actioncable.js
	// 1 ping is 2 sec interval, so detect stale when 2 ping missing.
	DEFAULT_STALE_THRESHOLD time.Duration = 6 * time.Second
)

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

func TestWebsocketURL

func TestWebsocketURL() *url.URL

Types

type ChannelIdentifier

type ChannelIdentifier struct {
	// contains filtered or unexported fields
}

func NewChannelIdentifier

func NewChannelIdentifier(channelName string, params map[string]interface{}) *ChannelIdentifier

func (*ChannelIdentifier) Equals

func (self *ChannelIdentifier) Equals(other *ChannelIdentifier) bool

func (*ChannelIdentifier) MarshalJSON

func (c *ChannelIdentifier) MarshalJSON() ([]byte, error)

Implements json.Marshaler#MarshalJSON()

func (*ChannelIdentifier) UnmarshalJSON

func (c *ChannelIdentifier) UnmarshalJSON(doubleEncodedData []byte) error

Implements json.Marshaler#UnmarshalJSON()

type Consumer

type Consumer struct {
	Subscriptions *Subscriptions
	// contains filtered or unexported fields
}

func CreateConsumer

func CreateConsumer(url *url.URL, opts *ConsumerOptions) (*Consumer, error)

Passing in nil options will cause it to create the consumer with the default options.

func (*Consumer) Connect

func (c *Consumer) Connect(ctx context.Context) error

func (*Consumer) Disconnect

func (c *Consumer) Disconnect()

type ConsumerOptions

type ConsumerOptions struct {
	// contains filtered or unexported fields
}

func NewConsumerOptions

func NewConsumerOptions() *ConsumerOptions

func (*ConsumerOptions) Header

func (o *ConsumerOptions) Header() *http.Header

func (*ConsumerOptions) SetHeader

func (o *ConsumerOptions) SetHeader(h *http.Header)

type DisconnectError

type DisconnectError struct {
	// contains filtered or unexported fields
}

func (*DisconnectError) Error

func (de *DisconnectError) Error() string

func (*DisconnectError) Reconnect

func (de *DisconnectError) Reconnect(defValue bool) bool

type Event

type Event struct {
	Type       string             `json:"type"`
	Message    json.RawMessage    `json:"message"`
	Reason     json.RawMessage    `json:"reason"`
	Reconnect  json.RawMessage    `json:"reconnect"`
	Data       json.RawMessage    `json:"data"`
	Identifier *ChannelIdentifier `json:"identifier"`
}

func NewNilEvent

func NewNilEvent() *Event

func (*Event) GetReason

func (e *Event) GetReason(reason *string) bool

func (*Event) GetReconnect

func (e *Event) GetReconnect(reconnect *bool) bool

type Logger

type Logger interface {
	Debug(string)
	Debugf(string, ...interface{})

	Info(string)
	Infof(string, ...interface{})

	Warn(string)
	Warnf(string, ...interface{})

	Error(string)
	Errorf(string, ...interface{})
}

func NewStandardLibLogger

func NewStandardLibLogger(l *log.Logger) Logger

type NilLogger

type NilLogger struct {
}

func (*NilLogger) Debug

func (l *NilLogger) Debug(message string)

func (*NilLogger) Debugf

func (l *NilLogger) Debugf(message string, args ...interface{})

func (*NilLogger) Error

func (l *NilLogger) Error(message string)

func (*NilLogger) Errorf

func (l *NilLogger) Errorf(message string, args ...interface{})

func (*NilLogger) Info

func (l *NilLogger) Info(message string)

func (*NilLogger) Infof

func (l *NilLogger) Infof(message string, args ...interface{})

func (*NilLogger) Warn

func (l *NilLogger) Warn(message string)

func (*NilLogger) Warnf

func (l *NilLogger) Warnf(message string, args ...interface{})

type StandardLibLogger

type StandardLibLogger struct {
	// contains filtered or unexported fields
}

func (*StandardLibLogger) Debug

func (l *StandardLibLogger) Debug(message string)

func (*StandardLibLogger) Debugf

func (l *StandardLibLogger) Debugf(message string, args ...interface{})

func (*StandardLibLogger) Error

func (l *StandardLibLogger) Error(message string)

func (*StandardLibLogger) Errorf

func (l *StandardLibLogger) Errorf(message string, args ...interface{})

func (*StandardLibLogger) Info

func (l *StandardLibLogger) Info(message string)

func (*StandardLibLogger) Infof

func (l *StandardLibLogger) Infof(message string, args ...interface{})

func (*StandardLibLogger) Warn

func (l *StandardLibLogger) Warn(message string)

func (*StandardLibLogger) Warnf

func (l *StandardLibLogger) Warnf(message string, args ...interface{})

type Subscription

type Subscription struct {
	Identifier *ChannelIdentifier
	NotifyCh   chan *SubscriptionEvent
	// contains filtered or unexported fields
}

func (*Subscription) Perform

func (s *Subscription) Perform(action string, data map[string]interface{})

func (*Subscription) Send

func (s *Subscription) Send(data map[string]interface{})

func (*Subscription) SetHandler

func (s *Subscription) SetHandler(h SubscriptionEventHandler)

func (*Subscription) Unsubscribe

func (s *Subscription) Unsubscribe()

type SubscriptionEvent

type SubscriptionEvent struct {
	Error      error
	Type       SubscriptionEventType
	Message    map[string]interface{}
	RawMessage *json.RawMessage
}

func (*SubscriptionEvent) ReadJSON

func (s *SubscriptionEvent) ReadJSON(v interface{}) error

type SubscriptionEventHandler

type SubscriptionEventHandler interface {
	OnConnected(*SubscriptionEvent)
	OnDisconnected(*SubscriptionEvent)
	OnRejected(*SubscriptionEvent)
	OnReceived(*SubscriptionEvent)
	OnError(error)
}

type SubscriptionEventType

type SubscriptionEventType string
const (
	Connected    SubscriptionEventType = SubscriptionEventType("connected")
	Disconnected SubscriptionEventType = SubscriptionEventType("disconnected")
	Rejected     SubscriptionEventType = SubscriptionEventType("rejected")
	Received     SubscriptionEventType = SubscriptionEventType("received")
	Error        SubscriptionEventType = SubscriptionEventType("ERROR")
)

type Subscriptions

type Subscriptions struct {
	// contains filtered or unexported fields
}

func (*Subscriptions) Create

func (s *Subscriptions) Create(channelIdentifier *ChannelIdentifier) (*Subscription, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL