hyperion

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 9 Imported by: 0

README

Hyperion

Easy to use websocket library

This is a wrapper for https://github.com/gorilla/websocket

Examples

Initialize Hyperion with the default config:

h := hyperion.Default()

Or use a custom config:

h := hyperion.New(&hyperion.Config{
    PingInterval: 30 * time.Second,
    ReadTimeout: 60 * time.Second,
    WriteTimeout: 60 * time.Second,
    // ...
})

Server with net/http

h := hyperion.Default()

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "index.html")
})

http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
    h.Upgrade(w, r)
})

h.HandleMessage(func(c *hyperion.Connection, m hyperion.Message) {
    // ... handle messages from the client
})

log.Fatal(http.ListenAndServe(":8080", nil))

Client

h := hyperion.Default()

conn, _, _ := h.Dial("ws://localhost:8080/ws", nil)

h.HandleMessage(func(conn *hyperion.Connection, m hyperion.Message) {
	// ... handle messages from the server
})

conn.WriteString("Hello World!")

You can find more examples here.

ToDo

  • Builtin ratelimitter

  • HTTP long polling support

  • Testing with Autobahn test suite

Documentation

Index

Constants

View Source
const (
	CloseNormalClosure           = websocket.CloseNormalClosure
	CloseGoingAway               = websocket.CloseGoingAway
	CloseProtocolError           = websocket.CloseProtocolError
	CloseUnsupportedData         = websocket.CloseUnsupportedData
	CloseNoStatusReceived        = websocket.CloseNoStatusReceived
	CloseAbnormalClosure         = websocket.CloseAbnormalClosure
	CloseInvalidFramePayloadData = websocket.CloseInvalidFramePayloadData
	ClosePolicyViolation         = websocket.ClosePolicyViolation
	CloseMessageTooBig           = websocket.CloseMessageTooBig
	CloseMandatoryExtension      = websocket.CloseMandatoryExtension
	CloseInternalServerErr       = websocket.CloseInternalServerErr
	CloseServiceRestart          = websocket.CloseServiceRestart
	CloseTryAgainLater           = websocket.CloseTryAgainLater
	CloseTLSHandshake            = websocket.CloseTLSHandshake
)

Using defined codes from github.com/gorilla/websocket to make them accessible through this lib Close codes defined in RFC 6455, section 11.7.

View Source
const (
	DefaultPingInterval     = 15 * time.Second
	DefaultWriteTimeout     = 30 * time.Second
	DefaultReadTimeout      = 30 * time.Second
	DefaultHandshakeTimeout = 15 * time.Second
	DefaultReadBufferSize   = 1024
	DefaultWriteBufferSize  = 1024
)

Default values used in hyperion.Default()

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	PingInterval    time.Duration
	WriteTimeout    time.Duration
	ReadTimeout     time.Duration
	ReadBufferSize  int
	WriteBufferSize int
	CheckOrigin     func(r *http.Request) bool
	Compression     bool
}

type Connection

type Connection struct {

	// 0 if connection isn't closed
	CloseCode int

	// The HTTP request that established the connection
	HttpUpgradeRequest *http.Request
	// contains filtered or unexported fields
}

func (*Connection) Close added in v0.1.2

func (c *Connection) Close() error

func (*Connection) IsClosed added in v0.1.1

func (c *Connection) IsClosed() bool

func (*Connection) WriteBytes

func (c *Connection) WriteBytes(b []byte) error

func (*Connection) WriteJSON

func (c *Connection) WriteJSON(v any) error

func (*Connection) WriteString

func (c *Connection) WriteString(s string) error

type ConnectionManager

type ConnectionManager struct {
	// contains filtered or unexported fields
}

type Hyperion

type Hyperion struct {
	Upgrader *websocket.Upgrader
	// contains filtered or unexported fields
}

func Default

func Default() *Hyperion

Uses the default configuration, use New() for a custom configuration

func New

func New(config *Config) *Hyperion

New Hyperion structure with defined config

func (*Hyperion) BroadcastBytes

func (h *Hyperion) BroadcastBytes(b []byte)

func (*Hyperion) BroadcastJSON

func (h *Hyperion) BroadcastJSON(v any) error

func (*Hyperion) BroadcastString

func (h *Hyperion) BroadcastString(s string)

func (*Hyperion) Dial added in v0.1.1

func (h *Hyperion) Dial(url string, header http.Header) (*Connection, *http.Response, error)

Opens up a Websocket to the provided URL

func (*Hyperion) HandleClose

func (h *Hyperion) HandleClose(handler func(*Connection, Message))

Set a function that will be called on close

func (*Hyperion) HandleMessage

func (h *Hyperion) HandleMessage(handler func(*Connection, Message))

Set a function that will be called if a new WebSocket message is received

func (*Hyperion) Upgrade added in v0.1.1

func (h *Hyperion) Upgrade(w http.ResponseWriter, r *http.Request) (*Connection, error)

type Message

type Message []byte

func (Message) String

func (m Message) String() string

Directories

Path Synopsis
examples
chat command
echo command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL