/websocket

Simple websocket library for golang

Primary LanguageGoMIT LicenseMIT

websocket

GoDoc Go Report Card codecov

Simple websocket library for golang

Installation

go get github.com/exelban/websocket

Example

Echo

package main

import (
	"github.com/exelban/websocket"
	"github.com/go-chi/chi"
	"net/http"
)

func main () {
	r := chi.NewRouter()
	wsServer := websocket.CreateAndRun()

	r.Get("/ws", wsServer.Handler)
	wsServer.On("echo", func(c *websocket.Conn, msg *websocket.Message) {
		c.Emit("echo", msg.Body)
	})

	http.ListenAndServe(":8080", r)
}

Channel

package main

import (
	"github.com/exelban/websocket"
	"github.com/go-chi/chi"
	"net/http"
)

func main () {
	r := chi.NewRouter()
	wsServer := websocket.CreateAndRun()

	ch := wsServer.NewChannel("test")

	wsServer.OnConnect(func(c *websocket.Conn) {
		ch.Add(c)
		ch.Emit("connection", "new connection come")
	})

	r.Get("/ws", wsServer.Handler)
	http.ListenAndServe(":8080", r)
}

HelloWorld

package main

import (
	"github.com/exelban/websocket"
	"github.com/go-chi/chi"
	"github.com/gobwas/ws"
	"net/http"
)

func main () {
	r := chi.NewRouter()
	wsServer := websocket.CreateAndRun()

	r.Get("/ws", wsServer.Handler)
	wsServer.OnMessage(func(c *websocket.Conn, h ws.Header, b []byte) {
		c.Send("Hello World")
	})

	http.ListenAndServe(":8080", r)
}

Benchmark

Autobahn

All tests was runned by Autobahn WebSocket Testsuite v0.8.0/v0.10.9. Results:

Code Name Status
1 Framing Pass
2 Pings/Pongs Pass
3 Reserved Bits Pass
4 Opcodes Pass
5 Fragmentation Pass
6 UTF-8 Handling Pass
7 Close Handling Pass
9 Limits/Performance Pass
10 Misc Pass
12 WebSocket Compression (different payloads) Unimplemented
13 WebSocket Compression (different parameters) Unimplemented

Licence

MIT License