faye

package module
v0.0.0-...-12a54d8 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: MIT Imports: 8 Imported by: 0

README

Faye + Go

Major websocket-oriented rewrite of roncohen/faye-go. Still experimental.

Usage


package main

import (
	"net/http"
	"os"
	"time"

	"github.com/apex/log"
	"github.com/apex/log/handlers/text"
	"github.com/dsablic/faye-go"
	"github.com/dsablic/faye-go/adapters"
	"github.com/dsablic/faye-go/protocol"
)

type extLogger struct {
	*log.Logger
}

func (l extLogger) Panicf(msg string, v ...interface{}) {
	l.Errorf(msg, v)
	panic(msg)
}

type callbacks struct{}

func (c callbacks) SubscribeValid(m *protocol.Message) bool {
	return true
}

func (c callbacks) PublishValid(m *protocol.Message) bool {
	return true
}

func main() {
	duration := time.Duration(10) * time.Second
	ctx := log.WithFields(log.Fields{})
	logger := extLogger{ctx.Logger}
	log.SetHandler(text.New(os.Stdout))
	statistics := make(chan faye.Counters)
	go func() {
    interval := uint(duration.Seconds())
		for c := range statistics {
			logger.Infof("Clients = %v, Publish = %v/s, Send = %v/s, Fail = %v/s",
				c.Clients, c.Published/interval, c.Sent/interval, c.Failed/interval)
		}
	}()
	engine := faye.NewEngine(logger, duration, statistics)
	server := faye.NewServer(logger, engine, callbacks{})
	http.Handle("/bayeux", adapters.FayeHandler(server))
	if err := http.ListenAndServe(":8000", nil); err != nil {
		panic("ListenAndServe: " + err.Error())
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counters

type Counters struct {
	Published           uint
	Sent                uint
	Clients             uint
	Failed              uint
	SubscriberByPattern uint
}

type Engine

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

func NewEngine

func NewEngine(logger utils.Logger, reapInterval time.Duration, statistics chan Counters) *Engine

func (*Engine) Connect

func (m *Engine) Connect(request *protocol.Message, client *protocol.Client, conn protocol.Connection)

func (*Engine) Disconnect

func (m *Engine) Disconnect(request *protocol.Message, client *protocol.Client, conn protocol.Connection)

func (*Engine) GetClient

func (m *Engine) GetClient(clientId uint32) *protocol.Client

func (*Engine) Handshake

func (m *Engine) Handshake(request *protocol.Message, conn protocol.Connection) uint32

func (*Engine) NewClient

func (m *Engine) NewClient(conn protocol.Connection) *protocol.Client

func (*Engine) Publish

func (m *Engine) Publish(request *protocol.Message, conn protocol.Connection)

func (*Engine) SubscribeClient

func (m *Engine) SubscribeClient(request *protocol.Message, client *protocol.Client)

func (*Engine) UnsubscribeClient

func (m *Engine) UnsubscribeClient(request *protocol.Message, client *protocol.Client)

type Server

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

func NewServer

func NewServer(logger utils.Logger, engine *Engine, validator Validator) *Server

func (*Server) HandleRequest

func (s *Server) HandleRequest(msges interface{}, conn protocol.Connection)

func (*Server) Logger

func (s *Server) Logger() utils.Logger

type Validator

type Validator interface {
	SubscribeValid(*protocol.Message) bool
	PublishValid(*protocol.Message) bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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