wsevent

package module
v0.0.0-...-1b42245 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2016 License: MIT Imports: 14 Imported by: 28

README

WSEvent

Circle CI Coverage Status GoDoc License

WSEvent implements thread-safe event-driven communication similar to socket.io, on the top of Gorilla's WebSocket implementation.

Documentation

Overview

Package wsevent implements thread-safe event-driven communication similar to socket.IO, on the top of Gorilla's WebSocket implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	ID      string        //Session ID
	Request *http.Request //http Request when connection was upgraded
	Token   *jwt.Token    //if any
	// contains filtered or unexported fields
}

Client represents a server-side client

func (*Client) Close

func (c *Client) Close()

func (*Client) Emit

func (c *Client) Emit(data string)

A thread-safe variant of WriteMessage

func (*Client) EmitJSON

func (c *Client) EmitJSON(v interface{}) error

A thread-safe variant of EmitJSON

type Receiver

type Receiver interface {
	Name(string) string
}

A Receiver interface implements the Name method, which returns a name for the event, given a registered function's name.

type Server

type Server struct {

	//Called when the websocket connection closes. The disconnected client's
	//session ID is sent as an argument
	OnDisconnect func(string, *jwt.Token)

	// Used to wait for all requests to complete
	Requests *sync.WaitGroup
	// contains filtered or unexported fields
}

Server represents an RPC server

func NewServer

func NewServer(codec ServerCodec, defaultHandler interface{}) *Server

NewServer returns a new server

func (*Server) Broadcast

func (s *Server) Broadcast(room string, data string)

Broadcast given data to all clients in the given room

func (*Server) BroadcastJSON

func (s *Server) BroadcastJSON(room string, v interface{})

BroadcastJSON broadcasts the json encoding of v to all clients in room

func (*Server) Clients

func (s *Server) Clients() int64

Clients return the number of clients connected/added to s

func (*Server) Close

func (s *Server) Close()

func (*Server) Join

func (s *Server) Join(c *Client, r string)

Join adds a client to the given room

func (*Server) Leave

func (s *Server) Leave(client *Client, r string)

Leave removes the client from the given room

func (*Server) NewClient

func (s *Server) NewClient(upgrader ws.Upgrader, w http.ResponseWriter, r *http.Request) (*Client, error)

func (*Server) NewClientWithID

func (s *Server) NewClientWithID(upgrader ws.Upgrader, w http.ResponseWriter, r *http.Request, id string) (*Client, error)

func (*Server) On

func (s *Server) On(event string, f interface{})

On Registers a callback for the event string. It panics if the callback isn't valid

func (*Server) Register

func (s *Server) Register(rcvr Receiver)

Register is similar to net/rpc's Register, expect that rcvr needs to implement the Receiver interface

func (*Server) Rooms

func (s *Server) Rooms() map[string]int

Rooms returns a map of room name -> number of clients

func (*Server) RoomsJoined

func (s *Server) RoomsJoined(id string) []string

RoomsJoined returns an array of rooms the client c has been added to

type ServerCodec

type ServerCodec interface {
	//ReadName reads the received data and returns the method/event name
	ReadName([]byte) string
	//Unmarshal reads the recieved paramters in the provided object, and returns errors
	//if (any) while unmarshaling, which is then sent a reply
	Unmarshal([]byte, interface{}) error
	//Error wraps the error returned by Unmarshal into a json-marshalable object
	Error(error) interface{}
}

ServerCodec implements a codec for reading method/event names and their parameters.

Jump to

Keyboard shortcuts

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