Skip to content

pkgz/websocket

Repository files navigation

websocket

GoDoc Go Report Card Tests codecov

Simple websocket library for golang

Installation

go get github.com/pkgz/websocket

Example

Echo

package main

import (
	"context"
	"github.com/pkgz/websocket"
	"net/http"
)

func main() {
	wsServer := websocket.Start(context.Background())

	r := http.NewServeMux()
	r.HandleFunc("/ws", wsServer.Handler)

	wsServer.On("echo", func(c *websocket.Conn, msg *websocket.Message) {
		_ = c.Emit("echo", msg.Data)
	})

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

Channel

package main

import (
	"context"
	"github.com/pkgz/websocket"
	"net/http"
)

func main() {
	wsServer := websocket.Start(context.Background())

	r := http.NewServeMux()
	r.HandleFunc("/ws", wsServer.Handler)

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

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

Hello World

package main

import (
	"context"
	"github.com/pkgz/websocket"
	"github.com/gobwas/ws"
	"net/http"
)

func main () {
	r := http.NewServeMux()
	wsServer := websocket.Start(context.Background())

	r.HandleFunc("/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