dispatcher

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2016 License: MIT Imports: 7 Imported by: 1

README

Dispatcher

Go package for easier work with websockets.

Install

go get http://gopkg.in/aliukevicius/dispatcher.v1

Import

import gopkg.in/aliukevicius/dispatcher.v1

Example

Please check the example folder for details.

Server side

func createUserHandler() dispatcher.Handler {
	return func(conn *dispatcher.Conn, data interface{}) {

		uData := data.(map[string]interface{})

		user := newUser{
			ID:       101,
			Name:     uData["name"].(string),
			Email:    uData["email"].(string),
			Passowrd: "password",
		}

		conn.Emit("newUser", user)
	}
}

func timeBroadcaster(disp *dispatcher.Dispatcher) {
	for {
		time.Sleep(time.Second)
		disp.Broadcast("time", time.Now().Format("2006-01-02 15:04:05"))
	}
}

Client side

	d.On('newUser', function (data) {
		console.log("New user: ", data); 
	});

	msg = {
		name: "John Doe",
		email: "john@dispatcher.doe"
	}; 

	d.Emit("createUser", msg);

	d.Join("time", function (data) {
		console.log("Time: ", data);
	});

	d.Join("chat", function (data) {
		console.log("Chat message: ", data)
	});

	d.Broadcast("chat", "Awesome chat message!");

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClientJs = []byte(`var Dispatcher = (function () { 

    var conf = {
        onJoin: null,
        onLeve: null,
        handlers: {},
        roomHandlers: {},
        conn: null
    };    

    function Dispatcher(endpoint) {

        conf.conn = new WebSocket(endpoint); 

        conf.conn.onmessage = function (msg) {
            var data = JSON.parse(msg.data);

            if (data.s === true) {
                if (data.e == "broadcast") {
                    if (conf.roomHandlers.hasOwnProperty(data.m.r)) {
                        conf.roomHandlers[data.m.r](data.m.m);
                    }
                }
            } else {
                if (conf.handlers.hasOwnProperty(data.e)) {
                    conf.handlers[data.e](data.m);
                }
            }
        };        
    }    

    Dispatcher.prototype.OnConnect = function (cb) {
        conf.conn.onopen = cb;
    };

    Dispatcher.prototype.OnDisconnect = function (cb) {
        conf.conn.onclose = cb;
    };

    Dispatcher.prototype.OnJoin = function (cb) {
        conf.onJoin = cb;
    };  
    
    Dispatcher.prototype.OnLeve = function (cb) {
        conf.onLeve = cb;
    };

    Dispatcher.prototype.Emit = function (event, message) {

        msg = {
            e: event,
            s: false,
            m: message
        };        

        conf.conn.send(JSON.stringify(msg));
    };

    Dispatcher.prototype.EmitTo = function (recipient, event, message) {
        msg = {
            e: "emitTo",
            s: true,
            m: {
                e: event,
                r: recipient,
                m: message
            }
        };        

        conf.conn.send(JSON.stringify(msg));
    };

    Dispatcher.prototype.Broadcast = function (room, message) {
        msg = {
            e: "broadcast",
            s: true,
            m: {
                r: room,
                m: message
            }
        };        

        conf.conn.send(JSON.stringify(msg));
    };

    Dispatcher.prototype.On = function (event, cb) {
        conf.handlers[event] = cb;
    };

    Dispatcher.prototype.Join = function (room, cb) {
        conf.roomHandlers[room] = cb;

        msg = {
            e: "join",
            s: true,
            m: room
        };        

        conf.conn.send(JSON.stringify(msg));
    };

    Dispatcher.prototype.Leave = function (room) {
        msg = {
            e: "leave",
            s: true,
            m: room
        };        

        conf.conn.send(JSON.stringify(msg));
    };


    Dispatcher.prototype.Disconnect = function () {
        conf.conn.close();
    };

    return Dispatcher;
} ());`)

Functions

func DispatcherJs

func DispatcherJs(w http.ResponseWriter, r *http.Request)

DispatcherJs handles client js code requests

Types

type BroadcastMessage

type BroadcastMessage struct {
	Room    string      `json:"r"`
	Message interface{} `json:"m"`
}

type Conn

type Conn struct {
	ID string
	// contains filtered or unexported fields
}

func (*Conn) Emit

func (c *Conn) Emit(event string, message interface{}) error

Emit sends message for particular event

func (*Conn) On

func (c *Conn) On(event string, handler Handler)

On assigns handler for event

func (*Conn) OnClose

func (c *Conn) OnClose(h ConnCloseHandler)

OnClose subscribe to connection close

type ConnCloseHandler

type ConnCloseHandler func(*Conn)

type Dispatcher

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

func NewDispatcher

func NewDispatcher(config *DispatcherConfig) *Dispatcher

NewDispatcher create dispatcher

func (*Dispatcher) Broadcast

func (d *Dispatcher) Broadcast(room string, message interface{}) error

Broadcast send message to a room subscribers

func (*Dispatcher) Close

func (d *Dispatcher) Close(ConnectionID string) error

Close connection

func (*Dispatcher) EmitTo

func (d *Dispatcher) EmitTo(recipient string, event string, msg interface{}) error

EmitTo send message to all connections in the room

func (*Dispatcher) Handle

func (d *Dispatcher) Handle(w http.ResponseWriter, r *http.Request, h *http.Header) (*Conn, error)

Handle upgrades http connection to websocket connection

type DispatcherConfig

type DispatcherConfig struct {
	ReadBufferSize  int
	WriteBufferSize int
}

type Handler

type Handler func(*Conn, interface{})

type MessageData

type MessageData struct {
	Event   string      `json:"e"`
	System  bool        `json:"s"`
	Message interface{} `json:"m"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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