socketio

package module
v0.0.0-...-73e4950 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 23 Imported by: 0

README

SocketIO GoDoc

This Go language SocketIO library aims to support all past, current and future versions of the Socket.io (and Engine.io) protocols and servers.

The library currently supports the following versions:

SocketIO protocol EngineIO protocol/payload SocketIO Server EngineIO Server
v1 (unspecified) (unspecified) v1.0.x (unspecified)
v2 v2 / v2 v2.4.x v2.1.x
v3 v3 / v3 v3.0.x v3.6.x
v4 v4 / v4 v4.5.x v4.1.x
v5 v5.2.x
v6.x (same as v5.x)

Getting the correct features/protocols/versions included inside which SocketIO and EngineIO Server versions can be confusing at times, therefore some servers may initially be implemented incorrectly, or not have features implemented. Please open a ticket for any discrepancies.

This library is very new and we're looking for beta testers.

Contents

Install

go get github.com/njones/socketio

Example

A simple example: sending a message out when a client connects
import (
	"log"
	"net/http"
	"time"

	sio "github.com/LeorlikK/socketio"
	eio "github.com/LeorlikK/socketio/engineio"
	eiot "github.com/LeorlikK/socketio/engineio/transport"
	ser "github.com/LeorlikK/socketio/serialize"
)

func main() {
	port := ":3000"

	server := sio.NewServer(
		eio.WithPingInterval(300*1*time.Millisecond),
		eio.WithPingTimeout(200*1*time.Millisecond),
		eio.WithMaxPayload(1000000),
		eio.WithTransportOption(eiot.WithGovernor(1500*time.Microsecond, 500*time.Microsecond)),
	)

	// use a OnConnect handler for incoming "connection" messages
	server.OnConnect(func(socket *sio.SocketV4) error {

		canYouHear := ser.String("can you hear me?")
		extra := ser.String("abc")

		var questions = ser.Integer(1)
		var responses = ser.Map(map[string]interface{}{"one": "no"})

		// send out a message to the hello
		socket.Emit("hello", canYouHear, questions, responses, extra)

		return nil
	})

	log.Printf("serving port %s...\n", port)
	log.Fatal(http.ListenAndServe(port, server))
}
A more complicated example: emitting and listening to a custom event
import (
	"log"
	"net/http"
	"time"

	sio "github.com/LeorlikK/socketio"
	eio "github.com/LeorlikK/socketio/engineio"
	eiot "github.com/LeorlikK/socketio/engineio/transport"
	ser "github.com/LeorlikK/socketio/serialize"
)

// Define a custom wrapper 
type CustomWrap func(string, string) error

// Define your callback
func (cc CustomWrap) Callback(data ...interface{}) error {
	a, aOK := data[0].(string)
	b, bOK := data[1].(string)

	if !aOK || !bOK {
		return fmt.Errorf("bad parameters")
	}

	return cc(a, b)
}

func main() {
	port := ":3000"
    server := socketio.NewServer()
    server.OnConnect(func(socket *sio.SocketV4) error {
         // Implement your callback for a custom event
         socket.On("myEvent", CustomWrap(func(a string, b string) error{
            socket.emit("hello", a, b)
            return nil
         })
    }
	log.Printf("serving port %s...\n", port)
	log.Fatal(http.ListenAndServe(port, server))
}

TODO

The following is in no particular order. Please open an Issue for priority or open a PR to contribute to this list.

  • Flesh out all tests
  • Document all public functions
  • Documentation
  • Develop a Client
  • Develop a Redis Transport
  • Makefile for all individual version builds
  • Makefile for all individual version git commits
  • Complete SocketIO Version 4
  • Complete EIO Server Version 5
  • Complete EIO Server Version 6
  • Complete this README

License

The MIT license.

See the LICENSE file for more information

Documentation

Index

Constants

View Source
const (
	ErrScrubFailed            erro.StringF = "failed to scrub string:: %w"
	ErrFromRoomFailed         erro.StringF = "failed to get socket ids from room:: %w"
	ErrUnknownEventName       erro.String  = "unknown event name, the first field is not a string"
	ErrUnknownBinaryEventName erro.StringF = "unknown event name, expected a string but found %v (%[1]T)"
	ErrUnsupportedEventName   erro.StringF = "event name unsupported, cannot use the registered name %q as an event name"
	ErrUnexpectedData         erro.StringF = "expected an []interface{} or []string, found %T"
	ErrUnexpectedBinaryData   erro.StringF = "expected an []interface{} (binary array) or []string, found %T"
	ErrUnexpectedPacketType   erro.StringF = "unexpected %T"
	ErrNamespaceNotFound      erro.StringF = "namespace %q not found"
	ErrOnConnectSocket        erro.State   = "socket: invalid onconnect"
	ErrOnDisconnectSocket     erro.State   = "socket: invalid ondisconnect"
)
View Source
const (
	OnDisconnectEvent = "disconnect"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data = seri.Serializable

type Event

type Event = string

type Namespace

type Namespace = string

type Option

type Option = with.Option

func WithPath

func WithPath(path string) Option

WithPath changes the path when using the SocketIO engine in conjunction with EngineIO. Use the engineio.WithPath to change the path when only using EngineIO.

type OptionWith

type OptionWith = with.OptionWith

type Request

type Request struct {
	Method     string
	URL        *url.URL
	Header     http.Header
	Host       string
	RemoteAddr string
	RequestURI string
	// contains filtered or unexported fields
}

Request is a wrapped HTTP request object so that we expose only the things that are necessary,

func (*Request) Context

func (req *Request) Context() context.Context

func (*Request) Cookie

func (req *Request) Cookie(name string) (*http.Cookie, error)

func (*Request) Cookies

func (req *Request) Cookies() []*http.Cookie

func (*Request) Referer

func (req *Request) Referer() string

func (*Request) UserAgent

func (req *Request) UserAgent() string

func (*Request) WithContext

func (req *Request) WithContext(ctx context.Context) *Request

type Room

type Room = string

type Server

type Server = interface {
	ServeHTTP(http.ResponseWriter, *http.Request)
}

Server is the generic interface that's used to designate the socketID as a server so that it can be added to a http.Server instance.

type ServerV1

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

ServerV1 is the same as the javascript SocketIO v1.0 server.

func NewServerV1

func NewServerV1(opts ...Option) *ServerV1

NewServerV1 returns a new v1.0 SocketIO server

func (ServerV1) Emit

func (v1 ServerV1) Emit(event Event, data ...Data) error

Emit - sending to all connected clients

func (*ServerV1) In

func (v1 *ServerV1) In(room Room) inToEmit

func (*ServerV1) Of

func (v1 *ServerV1) Of(ns Namespace) inSocketV1

func (ServerV1) On

func (v1 ServerV1) On(event Event, callback eventCallback)

func (ServerV1) OnConnect

func (v1 ServerV1) OnConnect(callback onConnectCallbackVersion1)

func (ServerV1) OnDisconnect

func (v1 ServerV1) OnDisconnect(callback func(string))

func (*ServerV1) ServeHTTP

func (v1 *ServerV1) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the interface for applying a http request/response cycle. This handles errors that can be provided by the underlining serveHTTP method that uses errors.

func (*ServerV1) To

func (v1 *ServerV1) To(room Room) inToEmit

func (*ServerV1) With

func (v1 *ServerV1) With(opts ...Option)

type ServerV2

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

The 4th revision (included in socket.io@1.0.3...2.x.x) can be found here: https://github.com/socketio/socket.io-protocol/tree/v4

func NewServerV2

func NewServerV2(opts ...Option) *ServerV2

func (ServerV2) Emit

func (v2 ServerV2) Emit(event Event, data ...Data) error

func (*ServerV2) In

func (v2 *ServerV2) In(room Room) inToEmit

func (*ServerV2) Of

func (v2 *ServerV2) Of(ns Namespace) inSocketV2

func (ServerV2) On

func (v2 ServerV2) On(event Event, callback eventCallback)

func (ServerV2) OnConnect

func (v2 ServerV2) OnConnect(callback onConnectCallbackVersion2)

func (ServerV2) OnDisconnect

func (v2 ServerV2) OnDisconnect(callback func(string))

func (*ServerV2) ServeHTTP

func (v2 *ServerV2) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServerV2) To

func (v2 *ServerV2) To(room Room) inToEmit

func (*ServerV2) With

func (v2 *ServerV2) With(opts ...Option)

type ServerV3

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

func NewServerV3

func NewServerV3(opts ...Option) *ServerV3

func (ServerV3) Emit

func (v3 ServerV3) Emit(event Event, data ...Data) error

Emit - sending to all connected clients

func (*ServerV3) In

func (v3 *ServerV3) In(room Room) inToEmit

func (*ServerV3) Of

func (v3 *ServerV3) Of(ns Namespace) inSocketV3

func (ServerV3) On

func (v3 ServerV3) On(event Event, callback eventCallback)

func (ServerV3) OnConnect

func (v3 ServerV3) OnConnect(callback onConnectCallbackVersion3)

func (ServerV3) OnDisconnect

func (v3 ServerV3) OnDisconnect(callback func(string))

func (*ServerV3) ServeHTTP

func (v3 *ServerV3) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServerV3) To

func (v3 *ServerV3) To(room Room) inToEmit

func (*ServerV3) With

func (v3 *ServerV3) With(opts ...Option)

type ServerV4

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

func NewServer

func NewServer(opts ...Option) *ServerV4

func NewServerV4

func NewServerV4(opts ...Option) *ServerV4

func (ServerV4) Emit

func (v4 ServerV4) Emit(event Event, data ...Data) error

Emit - sending to all connected clients

func (*ServerV4) Except

func (v4 *ServerV4) Except(room ...Room) innTooExceptEmit

func (*ServerV4) In

func (v4 *ServerV4) In(room ...Room) innTooExceptEmit

func (*ServerV4) Of

func (v4 *ServerV4) Of(namespace Namespace) inSocketV4

func (ServerV4) On

func (v4 ServerV4) On(event Event, callback eventCallback)

func (ServerV4) OnConnect

func (v4 ServerV4) OnConnect(callback onConnectCallbackVersion4)

func (ServerV4) OnDisconnect

func (v4 ServerV4) OnDisconnect(callback func(string))

func (*ServerV4) ServeHTTP

func (v4 *ServerV4) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*ServerV4) To

func (v4 *ServerV4) To(room ...Room) innTooExceptEmit

func (*ServerV4) With

func (v4 *ServerV4) With(opts ...Option)

type SessionID

type SessionID = eios.ID

SocketID is am alias of a session id, so that we don't need to reference sessions through the package

type SocketID

type SocketID = sios.ID

type SocketV1

type SocketV1 struct {
	Connected bool
	// contains filtered or unexported fields
}

SocketV1 is the returned socket

func (*SocketV1) Broadcast

func (v1 *SocketV1) Broadcast() emit

func (*SocketV1) Compress

func (v1 *SocketV1) Compress(compress bool) broadcastEmit

func (*SocketV1) Emit

func (v1 *SocketV1) Emit(event Event, data ...Data) error

func (*SocketV1) ID

func (v1 *SocketV1) ID() SocketID

func (SocketV1) In

func (v1 SocketV1) In(room Room) inToEmit

func (*SocketV1) Join

func (v1 *SocketV1) Join(room Room) error

func (*SocketV1) Leave

func (v1 *SocketV1) Leave(room Room) error

func (SocketV1) Of

func (v1 SocketV1) Of(namespace Namespace) inSocketV1

Of - sending to all clients in namespace, including sender

func (SocketV1) On

func (v1 SocketV1) On(event Event, callback eventCallback)

func (SocketV1) OnConnect

func (v1 SocketV1) OnConnect(callback onConnectCallbackVersion1)

func (SocketV1) OnDisconnect

func (v1 SocketV1) OnDisconnect(callback func(string))

func (*SocketV1) Request

func (v1 *SocketV1) Request() *Request

func (SocketV1) To

func (v1 SocketV1) To(room Room) inToEmit

func (*SocketV1) Volatile

func (v1 *SocketV1) Volatile() broadcastEmit

type SocketV2

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

func (*SocketV2) Binary

func (v2 *SocketV2) Binary(binary bool) emit

func (*SocketV2) Broadcast

func (v2 *SocketV2) Broadcast() emit

func (*SocketV2) Compress

func (v2 *SocketV2) Compress(compress bool) emit

func (*SocketV2) Emit

func (v2 *SocketV2) Emit(event Event, data ...Data) error

func (*SocketV2) ID

func (v2 *SocketV2) ID() SocketID

func (SocketV2) In

func (v2 SocketV2) In(room Room) inToEmit

func (*SocketV2) Join

func (v2 *SocketV2) Join(room Room) error

func (*SocketV2) Leave

func (v2 *SocketV2) Leave(room Room) error

func (SocketV2) Of

func (v2 SocketV2) Of(namespace Namespace) inSocketV2

func (SocketV2) On

func (v2 SocketV2) On(event Event, callback eventCallback)

func (SocketV2) OnConnect

func (v2 SocketV2) OnConnect(callback onConnectCallbackVersion2)

func (SocketV2) OnDisconnect

func (v2 SocketV2) OnDisconnect(callback func(string))

func (*SocketV2) Request

func (v2 *SocketV2) Request() *Request

func (SocketV2) To

func (v2 SocketV2) To(room Room) inToEmit

func (*SocketV2) Volatile

func (v2 *SocketV2) Volatile() emit

type SocketV3

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

func (*SocketV3) Broadcast

func (v3 *SocketV3) Broadcast() emit

func (*SocketV3) Compress

func (v3 *SocketV3) Compress(compress bool) emit

func (*SocketV3) Emit

func (v3 *SocketV3) Emit(event Event, data ...Data) error

func (*SocketV3) ID

func (v3 *SocketV3) ID() SocketID

func (SocketV3) In

func (v3 SocketV3) In(room Room) inToEmit

In - sending to all clients in room, including sender

func (*SocketV3) Join

func (v3 *SocketV3) Join(room Room) error

func (*SocketV3) Leave

func (v3 *SocketV3) Leave(room Room) error

func (SocketV3) Of

func (v3 SocketV3) Of(namespace Namespace) inSocketV3

Of - sending to all clients in namespace, including sender

func (SocketV3) On

func (v3 SocketV3) On(event Event, callback eventCallback)

func (SocketV3) OnConnect

func (v3 SocketV3) OnConnect(callback onConnectCallbackVersion3)

func (SocketV3) OnDisconnect

func (v3 SocketV3) OnDisconnect(callback func(string))

func (*SocketV3) Request

func (v3 *SocketV3) Request() *Request

func (SocketV3) To

func (v3 SocketV3) To(room Room) inToEmit

To - sending to all clients in room, except sender

func (*SocketV3) Volatile

func (v3 *SocketV3) Volatile() emit

type SocketV4

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

func (*SocketV4) Broadcast

func (v4 *SocketV4) Broadcast() emit

func (*SocketV4) Compress

func (v4 *SocketV4) Compress(compress bool) emit

func (*SocketV4) Emit

func (v4 *SocketV4) Emit(event Event, data ...Data) error

func (SocketV4) Except

func (v4 SocketV4) Except(rooms ...Room) innTooExceptEmit

Except - sending to all clients in room, except sender

func (*SocketV4) Handshake

func (v4 *SocketV4) Handshake() handshakeV4

func (*SocketV4) ID

func (v4 *SocketV4) ID() SocketID

func (SocketV4) In

func (v4 SocketV4) In(rooms ...Room) innTooExceptEmit

In - sending to all clients in room, including sender

func (*SocketV4) Join

func (v4 *SocketV4) Join(room Room) error

func (*SocketV4) Leave

func (v4 *SocketV4) Leave(room Room) error

func (SocketV4) Of

func (v4 SocketV4) Of(namespace Namespace) inSocketV4

Of - sending to all clients in namespace, including sender

func (SocketV4) On

func (v4 SocketV4) On(event Event, callback eventCallback)

func (SocketV4) OnConnect

func (v4 SocketV4) OnConnect(callback onConnectCallbackVersion4)

func (SocketV4) OnDisconnect

func (v4 SocketV4) OnDisconnect(callback func(string))

func (*SocketV4) Request

func (v4 *SocketV4) Request() *Request

func (*SocketV4) Timeout

func (v4 *SocketV4) Timeout(dur time.Duration) emit

func (SocketV4) To

func (v4 SocketV4) To(rooms ...Room) innTooExceptEmit

To - sending to all clients in room, except sender

func (*SocketV4) Volatile

func (v4 *SocketV4) Volatile() emit

Directories

Path Synopsis
adaptor
internal
Package protocol provides a object representation of a socket.io packet.
Package protocol provides a object representation of a socket.io packet.

Jump to

Keyboard shortcuts

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