websocket

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2018 License: MIT Imports: 11 Imported by: 0

README

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", []byte("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

Documentation

Overview

Package websocket implements the WebSocket protocol with additional functions.

Examples

Echo server:

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)
}

Websocket with group:

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", []byte("new connection come"))
	})

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

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 represent group of connections (similar to group in socket.io).

func (*Channel) Add

func (c *Channel) Add(conn *Conn)

Add add connection to channel.

func (*Channel) Count

func (c *Channel) Count() int

Count return number of connections in channel.

func (*Channel) Emit

func (c *Channel) Emit(name string, body interface{}) error

Emit emits message to all connections in channel.

func (*Channel) ID

func (c *Channel) ID() string

ID return channel id.

func (*Channel) Remove

func (c *Channel) Remove(conn *Conn)

Remove remove connection from channel.

type Conn

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

Conn websocket connection

func (*Conn) Close

func (c *Conn) Close() error

Close closing websocket connection.

func (*Conn) Emit

func (c *Conn) Emit(name string, body interface{}) error

Emit emit message to connection.

func (*Conn) Send

func (c *Conn) Send(data interface{}) error

Send send data to connection.

func (*Conn) Write

func (c *Conn) Write(h ws.Header, b []byte) error

Write write byte array to connection.

type HandlerFunc

type HandlerFunc func(c *Conn, msg *Message)

HandleFunc is a type for handle function all function which has callback have this struct as first element returns pointer to connection its give opportunity to close connection or emit message to exactly this connection.

type Message

type Message struct {
	Name string      `json:"name"`
	Body interface{} `json:"body"`
}

Message is a struct for data which sending between application and clients. Name using for matching callback function in On function. Body will be transformed to byte array and returned to callback.

type Server

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

Server allow to keep connection list, broadcast channel and callbacks list.

func Create

func Create() *Server

Create a new websocket server handler with the provided options.

func CreateAndRun

func CreateAndRun() *Server

CreateAndRun instantly create and run websocket server.

func (*Server) Count

func (s *Server) Count() int

Count return number of active connections.

func (*Server) Emit

func (s *Server) Emit(name string, body interface{})

Emit emit message to all connections.

func (*Server) Handler

func (s *Server) Handler(w http.ResponseWriter, r *http.Request)

Handler get upgrade connection to RFC 6455 and starting listener for it.

func (*Server) NewChannel

func (s *Server) NewChannel(name string) *Channel

NewChannel create new channel and proxy channel delConn for handling connection closing.

func (*Server) On

func (s *Server) On(name string, f HandlerFunc)

On adding callback for message.

func (*Server) OnConnect

func (s *Server) OnConnect(f func(c *Conn))

Set function which will be called when new connections come.

func (*Server) OnDisconnect

func (s *Server) OnDisconnect(f func(c *Conn))

Set function which will be called when new connections come.

func (*Server) OnMessage

func (s *Server) OnMessage(f func(c *Conn, h ws.Header, b []byte))

OnMessage handling byte message. By default this function works as echo.

func (*Server) Run

func (s *Server) Run()

Run start go routine which listening for channels.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown must be called before application died its goes throw all connection and closing it and stopping all goroutines.

Jump to

Keyboard shortcuts

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