All Projects → fanux → Lhttp

fanux / Lhttp

Licence: mit
go websocket, a better way to buid your IM server

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Lhttp

Beast
HTTP and WebSocket built on Boost.Asio in C++11
Stars: ✭ 3,241 (+437.48%)
Mutual labels:  websocket, websockets
Websockets
Library for building WebSocket servers and clients in Python
Stars: ✭ 3,724 (+517.58%)
Mutual labels:  websocket, websockets
Microwebsrv2
The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
Stars: ✭ 295 (-51.08%)
Mutual labels:  websocket, websockets
Him Netty
开源的H5即时聊天系统 spring-boot + netty + protobuf + vue ~
Stars: ✭ 194 (-67.83%)
Mutual labels:  im, websocket
Kingim
KingIM是基于layim的websocket即时通讯系统,底层使用netty。
Stars: ✭ 538 (-10.78%)
Mutual labels:  im, websocket
Aiowebsocket
Async WebSocket Client. Advantage: Flexible Lighter and Faster
Stars: ✭ 263 (-56.38%)
Mutual labels:  websocket, websockets
Python Slack Sdk
Slack Developer Kit for Python
Stars: ✭ 3,307 (+448.42%)
Mutual labels:  websocket, websockets
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+120.73%)
Mutual labels:  im, websocket
Java Slack Sdk
Slack Developer Kit (including Bolt for Java) for any JVM language
Stars: ✭ 393 (-34.83%)
Mutual labels:  websocket, websockets
Wssip
Application for capturing, modifying and sending custom WebSocket data from client to server and vice versa.
Stars: ✭ 373 (-38.14%)
Mutual labels:  websocket, websockets
Gff
GFF is a imitation QQ communication project, based on high IOCP. GFF是模仿QQ通讯项目,通信基于SAEA.MessageSocket、SAEA.Http、SAEA.MVC实现
Stars: ✭ 162 (-73.13%)
Mutual labels:  im, websocket
Microwebsrv
A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
Stars: ✭ 420 (-30.35%)
Mutual labels:  websocket, websockets
Him Vue
开源的H5即时聊天系统 spring-boot + netty + protobuf + vue ~
Stars: ✭ 142 (-76.45%)
Mutual labels:  im, websocket
Node Slack Sdk
Slack Developer Kit for Node.js
Stars: ✭ 2,988 (+395.52%)
Mutual labels:  websocket, websockets
Light Push
轻量级推送服务和实时在线监控平台,同时用于开发即时通信系统,基于node的socket.io,支持web、android、ios客户端,支持移动端离线推送,可进行分布式部署
Stars: ✭ 128 (-78.77%)
Mutual labels:  im, websocket
Websocket Client
🔧 .NET/C# websocket client library
Stars: ✭ 297 (-50.75%)
Mutual labels:  websocket, websockets
Netty Websocket Spring Boot Starter
🚀 lightweight high-performance WebSocket framework ( 轻量级、高性能的WebSocket框架)
Stars: ✭ 885 (+46.77%)
Mutual labels:  im, websocket
Gochat
goim server write by golang !🚀
Stars: ✭ 1,144 (+89.72%)
Mutual labels:  im, websocket
Ttyd
Share your terminal over the web
Stars: ✭ 4,030 (+568.33%)
Mutual labels:  websocket, websockets
Tokio Tungstenite
Tokio binding for Tungstenite, the Lightweight stream-based WebSocket implementation
Stars: ✭ 392 (-34.99%)
Mutual labels:  websocket, websockets

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].