gowmb: github.com/owulveryck/gowmb Index | Examples | Files

package gowmb

import "github.com/owulveryck/gowmb"

Package gowmb (go Websocket Message Broker) manages message processing and broadcast between various clients, based on a segregation "tag".

Basics

The CreateHandler should be used to create a http.Handler type that can be used with the net/http (or any good http's muxer package).

Assuming you've defined a Messenger'c compatible message type, every client connected to the web-path server by the defined handler will receive a Serialize version of the Message each time the server's websocket detects an event.

Tip's and tricks

The broker does not validate the format of the message. The message is "acked" at the protocol level, but the broker does not advertize any client that a message has been discarded

Code:

package main

import (
    "encoding/json"
    "github.com/gorilla/mux"
    "github.com/owulveryck/gowmb"
    "log"
    "net/http"
    "strconv"
)

//message is the top envelop for message communication between nodes
type message struct {
    ID int `json:"id"`
}

// Createmessage creates a new message and returns a pointer
func createMessage() *message {
    return &message{}
}

// Serialize returns a byte array of the message
func (m *message) Serialize() ([]byte, error) {
    return json.Marshal(m)
}

// Set function updates the content of message m awwording to input n
// And it fills the Msg's interface Contract
func (m *message) Set(n []byte) error {
    type input struct {
        ID int `json:"int"`
    }
    var message input
    err := json.Unmarshal(n, &message)
    if err != nil {
        return err
    }
    m.ID = message.ID
    return nil
}

type tag int

func (t *tag) Parse(s string) error {
    v, err := strconv.Atoi(s)
    if err != nil {
        return err
    }
    *t = tag(v)
    return nil
}

func newTag() *tag {
    return new(tag)
}

func main() {
    router := mux.NewRouter().StrictSlash(true)

    handler := gowmb.CreateHandler(createMessage(), newTag(), "tag")
    router.
        Methods("GET").
        Path("/serveWs/{tag}").
        Name("WebSocket").
        HandlerFunc(handler)
    log.Fatal(http.ListenAndServe(":8080", router))

}

Index

Examples

Package Files

conn.go doc.go handler.go hub.go interfaces.go

func CreateHandler Uses

func CreateHandler(creator Messager, tag Tag, tagName string) func(http.ResponseWriter, *http.Request)

CreateHandler returns a http.handler. It takes as input a Messager interface

type Messager Uses

type Messager interface {
    Set(n []byte) error
    Serialize() ([]byte, error)
}

Messager is an interface. The message can be setted via the set method. The message is encoded by the Serialize method and sent through the sockets

type Tag Uses

type Tag interface {
    Parse(s string) error
}

The Tag for channel separation

Package gowmb imports 6 packages (graph). Updated 2016-07-21. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).