Categorygithub.com/njones/socketio
modulepackage
0.1.4
Repository: https://github.com/njones/socketio.git
Documentation: pkg.go.dev

# 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 protocolEngineIO protocol/payloadSocketIO ServerEngineIO Server
v1 (unspecified)(unspecified)v1.0.x(unspecified)
v2v2 / v2v2.4.xv2.1.x
v3v3 / v3v3.0.xv3.6.x
v4v4 / v4v4.5.xv4.1.x
v5v5.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/njones/socketio"
	eio "github.com/njones/socketio/engineio"
	eiot "github.com/njones/socketio/engineio/transport"
	ser "github.com/njones/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/njones/socketio"
	eio "github.com/njones/socketio/engineio"
	eiot "github.com/njones/socketio/engineio/transport"
	ser "github.com/njones/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

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
Package protocol provides a object representation of a socket.io packet.
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
NewServerV1 returns a new v1.0 SocketIO server.
No description provided by the author
No description provided by the author
No description provided by the author
WithPath changes the path when using the SocketIO engine in conjunction with EngineIO.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Request is a wrapped HTTP request object so that we expose only the things that are necessary,.
ServerV1 is the same as the javascript SocketIO v1.0 server.
The 4th revision (included in [email protected]) can be found here: https://github.com/socketio/socket.io-protocol/tree/v4.
No description provided by the author
No description provided by the author
SocketV1 is the returned socket.
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

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 aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author