lhttp

package module
v0.0.0-...-bf219b4 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2018 License: MIT Imports: 22 Imported by: 2

README

Your star is my power!! 🚀 ⭐ ⭐ ⭐ ⭐ ⭐

License MIT Go Report Card GoDoc Awesome

Discribe

lhttp is a http like protocol using websocket to provide long live, build your IM service quickly scalable without XMPP!

Everything is customizable.

简体中文
Features
  • simple easy but powerful!
  • fast, publish 10000 messages using 0.04s(single-core CPU,1G memory).
  • support cluster.
  • easy to customize and expansion.
  • work well with HTTP. So LHTTP can work with others language like PHP java python etc,.
A simple chat room demo

chat-demo with lhttp javascript sdk we complete a simple chat room within 40 lines code!!

SDKs
Header filter development
Protocol stack:
+--------------------+
|       lhttp        |
+--------------------+
|     websocket      |
+--------------------+
|        TCP         |
+--------------------+
Architecture
        +---------------------------------------+
        |    message center cluster (gnatsd)    |
        +---------------------------------------+
 ........|.................|...............|..................
| +-------------+   +-------------+   +-------------+        | 
| |lhttp server |   |lhttp server |   |lhttp server |   ...  |  lhttp server cluster
| +-------------+   +-------------+   +-------------+        | 
 .....|..........._____|  |___.............|  |_________......
      |          |            |            |            |       <----using websocket link
 +--------+  +--------+   +--------+   +--------+   +--------+   
 | client |  | client |   | client |   | client |   | client |   
 +--------+  +--------+   +--------+   +--------+   +--------+  
Quick start
go get github.com/nats-io/nats
go get github.com/fanux/lhttp

We need run gnatsd first:

cd bin
./gnatsd &
./lhttpd 

Open anohter bash run lhttpClient, then input your command:

cd bin
./lhttpClient
Ship on docker
$ docker build -t lhttp:latest .
$ docker run -p 9090:9090 -p 8081:8081 lhttp:latest

Open two windows in your browser, enter http://localhost:9090.

Lhttp server port is 8081, your own websocket client can connect to ws://localhost:8081

Enjoy the chat...

Alternative, pull image from docker hub.

$ docker run -p 9090:9090 -p 8081:8081 fanux/lhttp:latest
Protocol
LHTTP/1.0 Command\r\n                --------start line, define command, and protocol [protocol/version] [command]\r\n
Header1:value\r\n                    --------headers
Header2:value\r\n
\r\n
body                                 --------message body

for example:

LHTTP/1.0 chat\r\n
content-type:json\r\n
publish:channel_jack\r\n
\r\n
{
    to:jack,
    from:mike,
    message:hello jack,
    time:1990-1210 5:30:48
}
Usage

define your processor, you need combine BaseProcessor

type ChatProcessor struct {
    *lhttp.BaseProcessor
}

if you don't like BaseProcessor, define your struct witch must has OnOpen(*WsHandler) OnClose(*WsHandler) method like this:(don't recommand)

type ChatProcessor struct {
}
func (p ChatProcessor)OnOpen(h *WsHandler) {
    //your logic
}
func (p ChatProcessor)OnClose(h *WsHandler) {
    //your logic
}
func (p ChatProcessor)OnMessage(h *WsHandler) {
    //your logic
}

regist your processor

lhttp.Regist("chat",&ChatProcessor{&lhttp.BaseProcessor{}})

then if command is "chat" ChatProcessor will handle it

define your onmessage handle

func (p *ChatProcessor)OnMessage(h *WsHandler) {
    h.Send(h.GetBody())
}
Start websocket server
http.Handler("/echo",lhttp.Handler(lhttp.StartServer))
http.ListenAndServe(":8081")
Example , echo
type ChatProcessor struct {
    *lhttp.BaseProcessor
}

func (p *ChatProcessor) OnMessage (h *lhttp.WsHandler) {
    log.Print("on message :", h.GetBody())
    h.Send(h.GetBody())
}

func main(){
    lhttp.Regist("chat", &ChatProcessor{&lhttp.BaseProcessor{}})

    http.Handle("/echo",lhttp.Handler(lhttp.StartServer))
    http.ListenAndServe(":8081",nil)
}

Test

open websocketServer and run:

cd websocketServer
go run test.go

as we can see, both of the new headers are added and new command is set by the server. If we don't set a header or command ,then they will return the same result as they requested.

open an other bash, and run client in websocketClient

cd websocketClient
go run test.go
Subscribe/Publish

client1:

LHTTP/1.0 command\r\n
subscribe:channelID\r\n
\r\n
body optional

client2:

LHTTP/1.0 command\r\n
publish:channelID\r\n
\r\n
body require

client1:

LHTTP/1.0 command\r\n
unsubscribe:channelID\r\n
\r\n
body optional

client2 publish a message by channelID, client1 subscribe it, so client 1 will receive the message. if client1 send unsubscribe channelID, he will not receive message any more in channelID

support multiple channelID:

LHTTP/1.0 chat\r\n
subscribe:channelID1 channelID2 channelID3\r\n
\r\n
Using HTTP publish message!

lhttp support publish message by standard HTTP. URL: /publish . method: POST . body: use lhttp publishes message as HTTP body. for example I want send a message to who subscribe channel_test by HTTP.

    resp,err := http.POST("https://www.yourserver.com/publish", "text/plain",
    "LHTTP/1.0 chat\r\npublish:channel_test\r\n\r\nhello channel_test guys!")

when lhttp server receive this message, will publish whole body to channel_test.

your can use Publish function in tools.go

//func Publish(channelID []string, command string, header map[string]string, body string) (err error) {
//}
//send message to who subscribe mike.

Publish("mike", "yourCommand", nil, "hello mike!")
Upstream

we can use lhttp as a proxy:

LHTTP/1.0 command\r\n
upstream:POST http://www.xxx.com\r\n
\r\n
body

lhttp will use hole message as http body, post to http://www.xxx.com if method is GET, lhttp send http GET request ignore lhttp message body:

LHTTP/1.0 command\r\n
upstream:GET http://www.xxx.com?user=user_a&age=26\r\n
\r\n
body
This case will show you about upstream proxy:

jack use lhttp chat with mike, lhttp is third part module, we can't modify lhttp server but we want to save the chat record, how can we do?

        +----+                  +----+
        |jack|                  |mike|
        +----+                  +----+
         |_____________    _______|
                       |  |
                   +------------+
                   |lhttp server|
                   +------------+
                         |(http request with chat record)
                         V
                   +------------+
                   | http server|  upstream server(http://www.xxx.com/record)
                   +------------+
                   (save chat record)
    

jack: MESSAGE_UPSTREAM

LHTTP/1.0 chat\r\n
upstream:POST http://www.xxx.com/record\r\n
publish:channel_mike\r\n
\r\n
hello mike,I am jack

mike:

LHTTP/1.0 chat\r\n
subscribe:channel_mike\r\n
\r\n

when jack send publish message, not only mike will receive the message, the http server will also receive it. witch http body is:MESSAGE_UPSTREAM, so http server can do anything about message include save the record

Multipart data

for example a file upload message, the multipart header record the offset of each data part, each part can have it own headers

LHTTP/1.0 upload\r\n
multipart:0 56\r\n
\r\n
content-type:text/json\r\n
\r\n
{filename:file.txt,fileLen:5}
content-type:text/plain\r\n
\r\n
hello
content-type:text/json\r\n\r\n{filename:file.txt,fileLen:5}content-type:text/plain\r\n\r\nhello
^                                                          ^
|<---------------------first part------------------------->|<---------second part------------>|
0                                                          56                           

why not boundary but use offset? if use boundary lhttp need ergodic hole message, that behaviour is poor efficiency. instead we use offset to cut message

How to get multipart data

for example this is client message.

LHTTP/1.0 upload\r\nmultipart:0 14\r\n\r\nk1:v1\r\n\r\nbody1k2:v2\r\n\r\nbody2

server code:

type UploadProcessor struct {
	*lhttp.BaseProcessor
}

func (*UploadProcessor) OnMessage(ws *lhttp.WsHandler) {
	for m := ws.GetMultipart(); m != nil; m = m.GetNext() {
		log.Print("multibody:", m.GetBody(), " headers:", m.GetHeaders())
	}
}

//don't forget to regist your command processor

lhttp.Regist("upload", &UploadProcessor{&lhttp.BaseProcessor{}})

Partners

Documentation

Overview

you can define your handle to processing your private header before or after process message

Index

Examples

Constants

View Source
const (
	// ProtocolVersionHybi13
	ProtocolVersionHybi13 = 13
	// ProtocolVersionHybi
	ProtocolVersionHybi = ProtocolVersionHybi13
	// SupportedProtocolVersion
	SupportedProtocolVersion = "13"

	// ContinuationFrame
	ContinuationFrame = 0
	// TextFrame
	TextFrame = 1
	// BinaryFrame
	BinaryFrame = 2
	// CloseFrame
	CloseFrame = 8
	// PingFrame
	PingFrame = 9
	// PongFrame
	PongFrame = 10
	// UnknownFrame
	UnknownFrame = 255
)

Variables

View Source
var (
	ErrBadMaskingKey         = &ProtocolError{"bad masking key"}
	ErrBadPongMessage        = &ProtocolError{"bad pong message"}
	ErrBadClosingStatus      = &ProtocolError{"bad closing status"}
	ErrUnsupportedExtensions = &ProtocolError{"unsupported extensions"}
	ErrNotImplemented        = &ProtocolError{"not implemented"}
)
View Source
var (
	// HEADER_KEY_PUBLISH
	HEADER_KEY_PUBLISH = "publish"
	// HEADER_KEY_SUBSCRIBE
	HEADER_KEY_SUBSCRIBE = "subscribe"
	// HEADER_KEY_UNSUBSCRIBE
	HEADER_KEY_UNSUBSCRIBE = "unsubscribe"
	// HEADER_KEY_UPSTREAM
	HEADER_KEY_UPSTREAM = "upstream"
	// HEADER_KEY_MULTIPART
	HEADER_KEY_MULTIPART = "multipart"
)
View Source
var (
	// UPSTREAM_HTTP_METHOD_GET
	UPSTREAM_HTTP_METHOD_GET = "GET"
	// UPSTREAM_HTTP_METHOD_POST
	UPSTREAM_HTTP_METHOD_POST = "POST"
)
View Source
var (
	// ErrBadProtocolVersion
	ErrBadProtocolVersion   = &ProtocolError{"bad protocol version"}
	ErrBadScheme            = &ProtocolError{"bad scheme"}
	ErrBadStatus            = &ProtocolError{"bad status"}
	ErrBadUpgrade           = &ProtocolError{"missing or bad upgrade"}
	ErrBadWebSocketOrigin   = &ProtocolError{"missing or bad WebSocket-Origin"}
	ErrBadWebSocketLocation = &ProtocolError{"missing or bad WebSocket-Location"}
	ErrBadWebSocketProtocol = &ProtocolError{"missing or bad WebSocket-Protocol"}
	ErrBadWebSocketVersion  = &ProtocolError{"missing or bad WebSocket Version"}
	ErrChallengeResponse    = &ProtocolError{"mismatch challenge/response"}
	ErrBadFrame             = &ProtocolError{"bad frame"}
	ErrBadFrameBoundary     = &ProtocolError{"not on frame boundary"}
	ErrNotWebSocket         = &ProtocolError{"not websocket protocol"}
	ErrBadRequestMethod     = &ProtocolError{"bad method"}
	ErrNotSupported         = &ProtocolError{"not supported"}
)
View Source
var CRLF = "\r\n"

CRLF is the end of text line

View Source
var JSON = Codec{jsonMarshal, jsonUnmarshal}

JSON is a codec to send/receive JSON data in a frame from a WebSocket connection.

Trivial usage:

import "websocket"

type T struct {
	Msg string
	Count int
}

// receive JSON type T
var data T
websocket.JSON.Receive(ws, &data)

// send JSON type T
websocket.JSON.Send(ws, data)
View Source
var (
	MaxLength = 40960
)
View Source
var Message = Codec{marshal, unmarshal}

Message is a codec to send/receive text/binary data in a frame on WebSocket connection. To send/receive text frame, use string type. To send/receive binary frame, use []byte type.

Trivial usage:

import "websocket"

// receive text frame
var message string
websocket.Message.Receive(ws, &message)

// send text frame
message = "hello"
websocket.Message.Send(ws, message)

// receive binary frame
var data []byte
websocket.Message.Receive(ws, &data)

// send binary frame
data = []byte{0, 1, 2}
websocket.Message.Send(ws, data)
View Source
var (
	// ProcessorMax
	ProcessorMax = 40
)

Functions

func Origin

func Origin(config *Config, req *http.Request) (*url.URL, error)

Origin parses the Origin header in req. If the Origin header is not set, it returns nil and nil.

func Publish

func Publish(channelID []string, command string, header map[string]string, body string) (err error)

Publish message to channel using http

func Regist

func Regist(command string, p HandlerCallbacks)

func RegistHeadFilter

func RegistHeadFilter(h HeadFilterHandler)

func StartServer

func StartServer(ws *Conn)

Types

type Addr

type Addr struct {
	*url.URL
}

Addr is an implementation of net.Addr for WebSocket.

func (*Addr) Network

func (addr *Addr) Network() string

Network returns the network type for a WebSocket, "websocket".

type BaseProcessor

type BaseProcessor struct {
}

func (*BaseProcessor) OnClose

func (*BaseProcessor) OnClose(*WsHandler)

func (*BaseProcessor) OnMessage

func (*BaseProcessor) OnMessage(*WsHandler)

func (*BaseProcessor) OnOpen

func (*BaseProcessor) OnOpen(*WsHandler)

type Codec

type Codec struct {
	Marshal   func(v interface{}) (data []byte, payloadType byte, err error)
	Unmarshal func(data []byte, payloadType byte, v interface{}) (err error)
}

Codec represents a symmetric pair of functions that implement a codec.

func (Codec) Receive

func (cd Codec) Receive(ws *Conn, v interface{}) (err error)

Receive receives single frame from ws, unmarshaled by cd.Unmarshal and stores in v.

func (Codec) Send

func (cd Codec) Send(ws *Conn, v interface{}) (err error)

Send sends v marshaled by cd.Marshal as single frame to ws.

type Config

type Config struct {
	// A WebSocket server address.
	Location *url.URL

	// A Websocket client origin.
	Origin *url.URL

	// WebSocket subprotocols.
	Protocol []string

	// WebSocket protocol version.
	Version int

	// TLS config for secure WebSocket (wss).
	TlsConfig *tls.Config

	// Additional header fields to be sent in WebSocket opening handshake.
	Header http.Header
	// contains filtered or unexported fields
}

Config is a WebSocket configuration

func NewConfig

func NewConfig(server, origin string) (config *Config, err error)

NewConfig creates a new WebSocket config for client connection.

type Conn

type Conn struct {
	PayloadType byte
	// contains filtered or unexported fields
}

Conn represents a WebSocket connection.

func Dial

func Dial(url_, protocol, origin string) (ws *Conn, err error)

Dial opens a new client connection to a WebSocket.

Example

This example demonstrates a trivial client.

origin := "http://localhost/"
url := "ws://localhost:12345/ws"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
	log.Fatal(err)
}
if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
	log.Fatal(err)
}
var msg = make([]byte, 512)
var n int
if n, err = ws.Read(msg); err != nil {
	log.Fatal(err)
}
fmt.Printf("Received: %s.\n", msg[:n])
Output:

func DialConfig

func DialConfig(config *Config) (ws *Conn, err error)

DialConfig opens a new client connection to a WebSocket with a config.

func NewClient

func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error)

NewClient creates a new WebSocket client connection over rwc.

func (*Conn) Close

func (ws *Conn) Close() error

Close implements the io.Closer interface.

func (*Conn) Config

func (ws *Conn) Config() *Config

Config returns the WebSocket config.

func (*Conn) IsClientConn

func (ws *Conn) IsClientConn() bool

IsClientConn

func (*Conn) IsServerConn

func (ws *Conn) IsServerConn() bool

IsServerConn

func (*Conn) LocalAddr

func (ws *Conn) LocalAddr() net.Addr

LocalAddr returns the WebSocket Origin for the connection for client, or the WebSocket location for server.

func (*Conn) Read

func (ws *Conn) Read(msg []byte) (n int, err error)

Read implements the io.Reader interface: it reads data of a frame from the WebSocket connection. if msg is not large enough for the frame data, it fills the msg and next Read will read the rest of the frame data. it reads Text frame or Binary frame.

func (*Conn) RemoteAddr

func (ws *Conn) RemoteAddr() net.Addr

RemoteAddr returns the WebSocket location for the connection for client, or the Websocket Origin for server.

func (*Conn) Request

func (ws *Conn) Request() *http.Request

Request returns the http request upgraded to the WebSocket. It is nil for client side.

func (*Conn) SetDeadline

func (ws *Conn) SetDeadline(t time.Time) error

SetDeadline sets the connection's network read & write deadlines.

func (*Conn) SetReadDeadline

func (ws *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the connection's network read deadline.

func (*Conn) SetWriteDeadline

func (ws *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the connection's network write deadline.

func (*Conn) Write

func (ws *Conn) Write(msg []byte) (n int, err error)

Write implements the io.Writer interface: it writes data as a frame to the WebSocket connection.

type DialError

type DialError struct {
	*Config
	Err error
}

DialError is an error that occurs while dialling a websocket server.

func (*DialError) Error

func (e *DialError) Error() string

type Handler

type Handler func(*Conn)

Handler is a simple interface to a WebSocket browser client. It checks if Origin header is valid URL by default. You might want to verify websocket.Conn.Config().Origin in the func. If you use Server instead of Handler, you could call websocket.Origin and check the origin in your Handshake func. So, if you want to accept non-browser clients, which do not send an Origin header, set a Server.Handshake that does not check the origin.

Example

This example demonstrates a trivial echo server.

package main

import (
	"io"
	"net/http"

	"golang.org/x/net/websocket"
)

// Echo the data received on the WebSocket.
func EchoServer(ws *websocket.Conn) {
	io.Copy(ws, ws)
}

// This example demonstrates a trivial echo server.
func main() {
	http.Handle("/echo", websocket.Handler(EchoServer))
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		panic("ListenAndServe: " + err.Error())
	}
}
Output:

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface for a WebSocket

type HandlerCallbacks

type HandlerCallbacks interface {
	OnOpen(*WsHandler)
	OnClose(*WsHandler)
	OnMessage(*WsHandler)
}

type HandlerHub

type HandlerHub struct {
}

func (*HandlerHub) Add

func (h *HandlerHub) Add(connSetID string, w *WsHandler)

func (*HandlerHub) Delete

func (h *HandlerHub) Delete(w *WsHandler)

func (*HandlerHub) Get

func (h *HandlerHub) Get(connSetID string) *WsHandler

type HeadFilterBase

type HeadFilterBase struct{}

func (*HeadFilterBase) AfterRequestFilterHandle

func (*HeadFilterBase) AfterRequestFilterHandle(ws *WsHandler)

func (*HeadFilterBase) BeforeRequestFilterHandle

func (*HeadFilterBase) BeforeRequestFilterHandle(ws *WsHandler)

func (*HeadFilterBase) OnCloseFilterHandle

func (*HeadFilterBase) OnCloseFilterHandle(ws *WsHandler)

func (*HeadFilterBase) OnOpenFilterHandle

func (*HeadFilterBase) OnOpenFilterHandle(ws *WsHandler)

type HeadFilterHandler

type HeadFilterHandler interface {
	OnOpenFilterHandle(*WsHandler)
	BeforeRequestFilterHandle(*WsHandler)
	AfterRequestFilterHandle(*WsHandler)
	OnCloseFilterHandle(*WsHandler)
}

type Mq

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

func NewMq

func NewMq() *Mq

func (*Mq) Publish

func (mq *Mq) Publish(key string, v MqHandler) error

func (*Mq) Subscribe

func (mq *Mq) Subscribe(key string, v MqHandler) (*nats.Subscription, error)

func (*Mq) Unsubscribe

func (mq *Mq) Unsubscribe(handle interface{}) error

type MqHandler

type MqHandler interface{}

type ProtocolError

type ProtocolError struct {
	ErrorString string
}

ProtocolError represents WebSocket protocol errors.

func (*ProtocolError) Error

func (err *ProtocolError) Error() string

type Server

type Server struct {
	// Config is a WebSocket configuration for new WebSocket connection.
	Config

	// Handshake is an optional function in WebSocket handshake.
	// For example, you can check, or don't check Origin header.
	// Another example, you can select config.Protocol.
	Handshake func(*Config, *http.Request) error

	// Handler handles a WebSocket connection.
	Handler
}

Server represents a server of a WebSocket.

func (Server) ServeHTTP

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

ServeHTTP implements the http.Handler interface for a WebSocket

type WsHandler

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

func (*WsHandler) AddHeader

func (req *WsHandler) AddHeader(hkey, hvalue string)

if header already exist,update it

func (*WsHandler) GetBody

func (req *WsHandler) GetBody() string

func (*WsHandler) GetCommand

func (req *WsHandler) GetCommand() string

func (*WsHandler) GetHeader

func (req *WsHandler) GetHeader(hkey string) string

func (*WsHandler) GetMultipart

func (req *WsHandler) GetMultipart() *multipartBlock

func (*WsHandler) Send

func (req *WsHandler) Send(body string)

if you want change command or header ,using SetCommand or AddHeader

func (*WsHandler) SetCommand

func (req *WsHandler) SetCommand(s string)

func (*WsHandler) SetHeader

func (req *WsHandler) SetHeader(hkey, hvalue string)

type WsMessage

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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