Documentation ¶
Overview ¶
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
Example ¶
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)) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateHandler ¶
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