websocket

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: MIT Imports: 7 Imported by: 0

README

Concurrent Websocket - Golang

A high performance websocket implementation that uses netpoll with read and write concurrency to allow a high number of concurrent websocket connections.

Prerequisite

Requires Go 1.18 or later, because of https://github.com/golang/go/issues/29257.

Quick start

# assume the following codes in example.go file
$ cat example.go
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/blakerouse/concurrent-websocket"
)

func main() {
    r := gin.Default()

    concurrency := 128
    echo := func(c *websocket.Channel, op websocket.OpCode, data []byte) {
        // echo
        c.Send(op, data)
    }

    wh, _ := websocket.NewHandler(echo, concurrency, concurrency)
    r.GET("/ws", gin.WrapF(wh.UpgradeHandler))

    r.Run() // listen and serve on 0.0.0.0:8080
}
# run example.go and visit 0.0.0.0:8080/ws on browser
$ go run example.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

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

Channel is a websocket connection that messages are read from and written to.

func (*Channel) Close

func (c *Channel) Close()

Close the channel.

func (*Channel) Send

func (c *Channel) Send(op OpCode, data []byte)

Send a message over the channel. Once write concurrency of the handler is reached this method will block.

func (*Channel) SetOnClose

func (c *Channel) SetOnClose(callback func())

SetOnClose sets the callback to get called when the channel is closed.

type Handler

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

Handler is a high performance websocket handler that uses netpoll with read and write concurrency to allow a high number of concurrent websocket connections.

func NewHandler

func NewHandler(callback RecievedCallback, readPoolConcurrency int, writePoolConcurrency int) (*Handler, error)

NewHandler creates a new websocket handler.

func (*Handler) CreateChannel

func (h *Handler) CreateChannel(w http.ResponseWriter, r *http.Request) (*Channel, error)

CreateChannel upgrades the incoming http request into a websocket channel.

func (*Handler) UpgradeHandler

func (h *Handler) UpgradeHandler(w http.ResponseWriter, r *http.Request)

UpgradeHandler upgrades the incoming http request to become a websocket connection.

type OpCode

type OpCode ws.OpCode

OpCode represents an operation code.

const (
	OpText   OpCode = 0x1
	OpBinary OpCode = 0x2
)

Operation codes that will be used in the RecievedCallback. rfc6455 defines more operation codes but those are hidden by the implementation.

type RecievedCallback

type RecievedCallback func(c *Channel, op OpCode, data []byte)

RecievedCallback is the signature for the callback called when a message is recieved on a Channel.

Jump to

Keyboard shortcuts

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