msgkit

package module
v0.0.0-...-12dd089 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2018 License: MIT Imports: 9 Imported by: 1

README

msgkit

GoDoc

msgkit is a quick and easy websocket message handling package for Go.

Usage

package main

import (
	"log"
	"net/http"

	"github.com/tile38/msgkit"
)

func main() {
	// Initialize a msgkit handler
	s := msgkit.NewServer(nil)

	// Bind a response handler to any JSON message with the "type" of "Echo"
	s.On("echo", func(so *msgkit.Socket, msg string) {
		so.Send("echo", "Hello World!")
	})

	// Bind the handler to url path "/ws"
	http.Handle("/ws", s)

	// start serving on port 8000
	srv := &http.Server{Addr: ":8000"}
	log.Fatal(srv.ListenAndServe())
}

The Idea

The msgkit payload is a JSON payload containing AT LEAST a message type. Any websocket message with a "type" field will be passed to its respective handler defined in your go code. You can choose to nest payloads within another field in your JSON message or pass fields at the parent level.

Note: The "type" should indicate both your method AND resource if applicable.

A request for an account with ID 1234
{
    "type": "Account",
    "id:": 1234,
}
A message with the text "Hey guys!"
{
    "type": "Message",
    "text": "Hey guys!"
}
A request to create an account
{
    "type": "Create-Account",
    "account": {
        "name": "Mike",
        "username": "Mike1234",
        "password": "Goforlife997"
    }
}

The MIT License (MIT)

Copyright © 2018 Tile38, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const (
	// EventConnected is an event name that is fired when a client connects
	EventConnected = "connected"
	// EventDisconnected is an event name that is fired when a client disconnects
	EventDisconnected = "disconnected"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(so *Socket, msg *Message) error

HandlerFunc is a type that defines the function signature of a msgkit request handler

type Message

type Message struct {
	Type string `json:"type"`
	Data string `json:"data"`
}

Message is the structural representation of a msgkit message

func NewMessage

func NewMessage(t string, d ...string) *Message

NewMessage produces a new Message reference from the passed type and data

func ParseMessage

func ParseMessage(msgb []byte) *Message

ParseMessage parses and returns a new fully populated Message type

type Server

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

Server contains all required dependencies to run a msgkit websocket server

func NewServer

func NewServer(u *websocket.Upgrader) *Server

NewServer creates a new Server using the passed custom websocket upgrader

func (*Server) Broadcast

func (s *Server) Broadcast(name, msg string)

Broadcast sends the passed message to all clients

func (*Server) Handle

func (s *Server) Handle(name string, handler HandlerFunc)

Handle binds a handler for a specified type

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the primary websocket handler method and conforms to the http.Handler interface.

type Socket

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

Socket is msgkit socket connection containing context about the connection

func (*Socket) Context

func (s *Socket) Context() interface{}

Context returns the context on the Socket

func (*Socket) Request

func (s *Socket) Request() *http.Request

Request returns the original request used to create the Socket

func (*Socket) Send

func (s *Socket) Send(name string, msgs ...interface{}) error

Send broadcasts a message over the socket

func (*Socket) SetContext

func (s *Socket) SetContext(ctx interface{})

SetContext applies the passed context interface to the Socket

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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