Skip to content
/ gws Public

simple, fast, reliable websocket server & client, supports running over tcp/kcp/unix domain socket. keywords: ws, proxy, chat, go, golang...

License

Notifications You must be signed in to change notification settings

lxzan/gws

Repository files navigation

gws

event-driven websocket server

Build Status MIT licensed Go Version codecov Go Report Card

Highlight

  • zero dependency, not use channel but event driven
  • zero extra goroutine to manage connection
  • zero error to read/write operation, errors have been handled appropriately
  • built-in concurrent_map implementation
  • fully passes the WebSocket autobahn-testsuite

Attention

  • It's designed for api server, do not write big message
  • It's recommended not to enable data compression in the intranet
  • WebSocket Events are emitted synchronously, manage goroutines yourself

Core Interface

type Event interface {
	OnOpen(socket *Conn)
	OnError(socket *Conn, err error)
	OnClose(socket *Conn, code uint16, reason []byte)
	OnPing(socket *Conn, payload []byte)
	OnPong(socket *Conn, payload []byte)
	OnMessage(socket *Conn, message *Message)
}

Install

go get -v github.com/lxzan/gws@latest

Quick Start

package main

import (
	"fmt"
	"github.com/lxzan/gws"
	"net/http"
)

func main() {
	var config = &gws.Config{
		CompressEnabled:   true,
		CheckTextEncoding: true,
		MaxContentLength:  32 * 1024 * 1024,
	}
	var handler = new(WebSocket)
	http.HandleFunc("/connect", func(writer http.ResponseWriter, request *http.Request) {
		socket, err := gws.Accept(writer, request, handler, config)
		if err != nil {
			return
		}
		socket.Listen()
	})

	_ = http.ListenAndServe(":3000", nil)
}

type WebSocket struct{}

func (c *WebSocket) OnClose(socket *gws.Conn, code uint16, reason []byte) {
	fmt.Printf("onclose: code=%d, payload=%s\n", code, string(reason))
}

func (c *WebSocket) OnError(socket *gws.Conn, err error) {
	fmt.Printf("onerror: err=%s\n", err.Error())
}

func (c *WebSocket) OnOpen(socket *gws.Conn) {
	println("connected")
}

func (c *WebSocket) OnPing(socket *gws.Conn, payload []byte) {
	fmt.Printf("onping: payload=%s\n", string(payload))
	socket.WritePong(payload)
}

func (c *WebSocket) OnPong(socket *gws.Conn, payload []byte) {}

func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
	socket.WriteMessage(message.Typ(), message.Bytes())
	message.Close()
}

TLS

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/lxzan/gws"
)

func main() {
	app := gin.New()
	handler := new(WebSocket)
	app.GET("/connect", func(ctx *gin.Context) {
		socket, err := gws.Accept(ctx.Writer, ctx.Request, handler, nil)
		if err != nil {
			return
		}
		socket.Listen()
	})
	cert := "server.crt"
	key := "server.key"
	if err := app.RunTLS(":8443", cert, key); err != nil {
		panic(err)
	}
}

Test

cd examples/testsuite
mkdir reports
docker run -it --rm \
    -v ${PWD}/config:/config \
    -v ${PWD}/reports:/reports \
    crossbario/autobahn-testsuite \
    wstest -m fuzzingclient -s /config/fuzzingclient.json

Benchmark

  • machine: Ubuntu 20.04LTS VM (4C8T)
  • client: tcpkali
Server Connection Send Speed(msg/s) Payload size Download / Upload Bandwidth(Mbps)
gws 200 20000 2.34KiB 11614.442↓ 11556.101↑
gorilla 200 20000 2.34KiB 4244.398↓ 4188.711↑
gws 2000 500 2.34KiB 7906.156↓ 7914.797↑
gorilla 2000 500 2.34KiB 4263.545↓ 4260.077↑
gws 5000 50 2.34KiB 4941.188↓ 4943.667↑
gorilla 5000 50 2.34KiB -
gws 10000 10 2.34KiB 1980.124↓ 1977.561↑
gorilla 10000 10 2.34KiB 1972.556↓ 1979.981↑
gws 10000 20 2.34KiB 3952.788↓ 3959.341↑
gorilla 10000 20 2.34KiB -

- means exception

About

simple, fast, reliable websocket server & client, supports running over tcp/kcp/unix domain socket. keywords: ws, proxy, chat, go, golang...

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published