websocket

package module
v0.0.0-...-e6cc796 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: MIT Imports: 14 Imported by: 0

README

websocket

High performance websocket server

Example

package main

import (
    "github.com/demskie/websocket"
    xwebsocket "golang.org/x/net/websocket"
)

manager, err := websocket.NewManager("127.0.0.1:12345", nil)
if err != nil {
    panic(err)
}

manager.SetReadPoolSize(1024)
manager.SetUpgradeTimeout(10 * time.Second)
manager.SetHandler(func(s *Session) {
    buf := &bytes.Buffer{}
    for i := 0; i < 123; i++ {
        _, err := s.Receive(buf, 0)
        if err != nil {
            panic(err)
        }
        s.Write(buf.Bytes())
    }
})

url := "ws://127.0.0.1:12345"
origin := "http://localhost"
ws, err := xwebsocket.Dial(url, "", origin)
if err != nil {
    panic(err)
}

n, err := ws.Write([]byte("hello, world!\n"))
if err != nil {
    panic(err)
}

payload := make([]byte, n)
_, err = ws.Read(payload)
if err != nil {
    panic(err)
}

if bytes.Compare(payload, []byte("hello, world!\n")) != 0 {
    panic(string(payload))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

func NewManager

func NewManager(address string, tlsCfg *tls.Config) (*Manager, error)

func (*Manager) SetHandler

func (m *Manager) SetHandler(handler func(s *Session))

func (*Manager) SetReadPoolSize

func (m *Manager) SetReadPoolSize(size int)

func (*Manager) SetUpgradeTimeout

func (m *Manager) SetUpgradeTimeout(timeout time.Duration)

type Session

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

func (*Session) Close

func (s *Session) Close()

func (*Session) Headers

func (s *Session) Headers() http.Header

func (*Session) Host

func (s *Session) Host() string

func (*Session) LocalAddr

func (s *Session) LocalAddr() net.Addr

func (*Session) OnClose

func (s *Session) OnClose(callback func())

func (*Session) Receive

func (s *Session) Receive(buf *bytes.Buffer, byteLimit int64) ([]byte, error)

func (*Session) ReceiveWithTimeout

func (s *Session) ReceiveWithTimeout(t time.Duration, buf *bytes.Buffer, byteLimit int64) ([]byte, error)

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() net.Addr

func (*Session) Subprotocol

func (s *Session) Subprotocol() string

func (*Session) URI

func (s *Session) URI() string

func (*Session) Write

func (s *Session) Write(data []byte) error

Jump to

Keyboard shortcuts

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