simplews_go

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

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

Go to latest
Published: Aug 31, 2022 License: BSD-2-Clause Imports: 5 Imported by: 1

README

SimpleWS

SimpleWS inspired by socket.io is a websocket librairy written in Golang. The project was created because today, it's not exist a socket.io v4 librairy in Golang

Warning: SimpleWS is'nt work with socket.io, the protocol is completely different !!!

Create an instance

    simplews := simplews_go.New(&simplews_go.Opts{
  ReadBufferSize:  simplews_go.PtrInt(1024),
  WriteBufferSize: simplews_go.PtrInt(1024),
  Compression:     simplews_go.PtrBool(false),
  Base64:          simplews_go.PtrBool(true),
 })

 simplews.On("ping", func(c *ws.Customer, msg interface{}) {
  c.Emit("pong", msg)
 })

 simplews.OnConnect(func(consumer ws.Customer) error {
  consumer.Emit("hello_pipeline", "hello new customer !")
  return nil;
 })

 simplews.OnDisconnect(func(consumer ws.Customer, reason string) {
  fmt.Printf("disconnect: %s\n", reason)
 })

 http.Handle("/simple.ws", simplews)

Websocket Protocol

SimpleWS is ... simple. Each message is encoded in JSON with this format :

Key Type of value Description
Type integer [0-3] connect: 0, disconnect: 1, event: 2 and error: 3
Event string name of your event ("ping" or "pong")
Data object related to event
Message string Only for the error message or the reason for disconnection.

Note: All of useless keys can be delete

Example of websocket message with SimpleWS

{
  "type": 2,
  "event": "new_user",
  "data": {
    "name": "Antoine",
    "age": 20
  }
}

Finally, the JSON message will be encoded with base64 and compressed with GZIP.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PtrBool

func PtrBool(i bool) *bool

func PtrInt

func PtrInt(i int) *int

Types

type HandleType

type HandleType int

type Handles

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

type Opts

type Opts struct {
	ReadBufferSize  *int  `default:"1024"`
	WriteBufferSize *int  `default:"1024"`
	Compression     *bool `default:"true"`
	Base64          *bool `default:"false"`
	CheckOrigin     *func(r *http.Request) bool
}

type Server

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

func New

func New(opts *Opts) Server

func (*Server) Everyone

func (h *Server) Everyone()

func (*Server) On

func (h *Server) On(event string, f func(customer *ws.Customer, msg interface{}))

func (*Server) OnConnect

func (h *Server) OnConnect(f func(customer ws.Customer) error)

func (*Server) OnDisconnect

func (h *Server) OnDisconnect(f func(customer ws.Customer, reason string))

func (*Server) OnError

func (h *Server) OnError(f func(err error))

func (*Server) OnEvent

func (h *Server) OnEvent(event string, f func(customer *ws.Customer, msg interface{}))

func (Server) ServeHTTP

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

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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