ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - fanux/lhttp: go websocket, a better way to buid your IM server
go websocket, a better way to buid your IM server. Contribute to fanux/lhttp development by creating an account on GitHub.
Visit Site

GitHub - fanux/lhttp: go websocket, a better way to buid your IM server

GitHub - fanux/lhttp: go websocket, a better way to buid your IM server

Your star is my power!! :rocket: :star: :star: :star: :star: :star:

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

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory