redisocket

package module
v0.0.0-...-4ab831d Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: MIT Imports: 14 Imported by: 1

README

redisocket.v2

Go Report Card Build Status

Base on gorilla/websocket & garyburd/redigo

Implement By Observer pattern

Documention

Install

go get github.com/syhlion/redisocket.v2

Usaged

func TestEvent(d []byte) (data []byte, err error) {
	return d, nil
}

func main() {
	pool := redis.NewPool(func() (redis.Conn, error) {
		return redis.Dial("tcp", ":6379")
	}, 10)
	app := redisocket.NewHub(pool,false)

	err := make(chan error)
	go func() {
		err <- app.Listen()
	}()

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {

        client,err:= app.Upgrade(w, r, nil, "Scott", "appKey")
		if err != nil {
			log.Fatal("Client Connect Error")
			return
		}
		err = client.Listen(func(data []byte) (d []byte, err error) {
			return data,nil

		})
		log.Println(err, "http point")
		return
	})

	go func() {
		err <- http.ListenAndServe(":8888", nil)
	}()
	select {
	case e := <-err:
		log.Println(e)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultWebsocketOptional default config
	DefaultWebsocketOptional = WebsocketOptional{
		ScanInterval:   30 * time.Second,
		WriteWait:      10 * time.Second,
		PongWait:       60 * time.Second,
		PingPeriod:     (60 * time.Second * 9) / 10,
		MaxMessageSize: 512,
		Upgrader: websocket.Upgrader{
			CheckOrigin: func(r *http.Request) bool { return true },
		},
	}
)

Functions

This section is empty.

Types

type Auth

type Auth struct {
	Channels []string        `json:"channels"`
	UserId   string          `json:"user_id"`
	AppKey   string          `json:"app_key"`
	Remotes  map[string]bool `json:"remotes"`
}

type BatchData

type BatchData struct {
	Event string
	Data  []byte
}

BatchData push batch data struct

type Client

type Client struct {
	*sync.RWMutex
	// contains filtered or unexported fields
}

Client gorilla websocket wrap struct

func (*Client) AddChannel

func (c *Client) AddChannel(s string)

func (*Client) Close

func (c *Client) Close()

Close client. disconnect client

func (*Client) GetAuth

func (c *Client) GetAuth() Auth

func (*Client) Listen

func (c *Client) Listen(re ReceiveMsgHandler)

Listen client client start listen it's block method

func (*Client) Off

func (c *Client) Off(event string)

Off event. client off event

func (*Client) On

func (c *Client) On(event string, h EventHandler)

On event. client on event

func (*Client) Send

func (c *Client) Send(data []byte)

Send message. write msg to client

func (*Client) SetChannels

func (c *Client) SetChannels(s []string)

func (*Client) SocketId

func (c *Client) SocketId() string

func (*Client) Trigger

func (c *Client) Trigger(event string, p *Payload) (err error)

Trigger event. trigger client reigster event

type EventHandler

type EventHandler func(event string, payload *Payload) error

EventHandler event handler

type Hub

type Hub struct {
	ChannelPrefix string

	Config WebsocketOptional
	// contains filtered or unexported fields
}

Hub client hub

func NewHub

func NewHub(m *redis.Pool, log *logrus.Logger, debug bool) (e *Hub)

NewHub It's create a Hub

func (*Hub) Close

func (e *Hub) Close()

Close close hub & close every client

func (*Hub) CountOnlineUsers

func (e *Hub) CountOnlineUsers() (i int)

CountOnlineUsers return online user total

func (*Hub) Listen

func (e *Hub) Listen(channelPrefix string) error

Listen hub start it's block method

func (*Hub) Ping

func (e *Hub) Ping() (err error)

Ping ping redis server

func (*Hub) Upgrade

func (e *Hub) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header, uid string, prefix string, auth *Auth) (c *Client, err error)

Upgrade gorilla websocket wrap upgrade method

type Payload

type Payload struct {
	Len            int
	Data           []byte
	PrepareMessage *websocket.PreparedMessage
	IsPrepare      bool
	Event          string
}

Payload reciev from redis

type ReceiveMsgHandler

type ReceiveMsgHandler func([]byte) ([]byte, error)

ReceiveMsgHandler client receive msg

type Sender

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

Sender struct

func NewSender

func NewSender(m *redis.Pool) (e *Sender)

NewSender return sender send to hub

func (*Sender) AddChannel

func (s *Sender) AddChannel(channelPrefix, appKey string, uid string, channel string) (val int, err error)

AddChannel append channel to user channel list

func (*Sender) GetChannels

func (s *Sender) GetChannels(channelPrefix string, appKey string, pattern string) (channels []string, err error)

GetChannels get all sub channels

func (*Sender) GetOnline

func (s *Sender) GetOnline(channelPrefix string, appKey string) (online []string, err error)

GetOnline get all online user

func (*Sender) GetOnlineByChannel

func (s *Sender) GetOnlineByChannel(channelPrefix string, appKey string, channel string) (online []string, err error)

GetOnlineByChannel get all online user by channel

func (*Sender) Push

func (s *Sender) Push(channelPrefix, appKey string, event string, data []byte) (val int, err error)

Push push single data

func (*Sender) PushBatch

func (s *Sender) PushBatch(channelPrefix, appKey string, data []BatchData)

PushBatch push batch data

func (*Sender) PushToSid

func (s *Sender) PushToSid(channelPrefix, appKey string, uid string, data interface{}) (val int, err error)

PushToSid push to user socket id

func (*Sender) PushToUid

func (s *Sender) PushToUid(channelPrefix, appKey string, uid string, data interface{}) (val int, err error)

PushTo push to user socket

func (*Sender) ReloadChannel

func (s *Sender) ReloadChannel(channelPrefix, appKey string, uid string, channels []string) (val int, err error)

ReloadChannel reload user channel list

type Statistic

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

func (*Statistic) AddInMsg

func (s *Statistic) AddInMsg(byteLength int)

func (*Statistic) AddMem

func (s *Statistic) AddMem()

func (*Statistic) AddOutMsg

func (s *Statistic) AddOutMsg(byteLength int)

func (*Statistic) Flush

func (s *Statistic) Flush(t time.Time)

func (*Statistic) Run

func (s *Statistic) Run()

func (*Statistic) SubMem

func (s *Statistic) SubMem()

type User

type User interface {
	Trigger(event string, p *Payload) (err error)
	Close()
}

User client interface

type WebsocketOptional

type WebsocketOptional struct {
	ScanInterval   time.Duration
	WriteWait      time.Duration
	PongWait       time.Duration
	PingPeriod     time.Duration
	MaxMessageSize int64
	Upgrader       websocket.Upgrader
}

WebsocketOptional init websocket hub config

Jump to

Keyboard shortcuts

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