socketio

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 12 Imported by: 0

README

gofiber-socket.io

  • gofiber-socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework.
  • It using with web-framework Go Fiber
  • This library support socket.io-client version 3, 4 and only support websocket transport

Contents

Install

Install the package with:

go get github.com/doquangtan/gofiber-socket.io

Import it with:

import "github.com/doquangtan/gofiber-socket.io"

and use socketio as the package name inside the code.

Example

Please check more examples into folder in project for details. Examples

package main

import (
	socketio "github.com/doquangtan/gofiber-socket.io"
	"github.com/gofiber/fiber/v2"
)

func socketIoRoute(app fiber.Router) {
	io := socketio.New()

	io.OnAuthorization(func(params map[string]string) bool {
		// auth, ok := params["Authorization"]
		// if !ok {
		// 	return false
		// }
		return true
	})

	io.OnConnection(func(socket *socketio.Socket) {
		println("connect", socket.Nps, socket.Id)
		socket.Join("demo")
		io.To("demo").Emit("hello", socket.Id+" join us room...", "server message")

		socket.On("test", func(event *socketio.EventPayload) {
			socket.Emit("test", event.Data...)
		})

		socket.On("join-room", func(event *socketio.EventPayload) {
			if len(event.Data) > 0 && event.Data[0] != nil {
				socket.Join(event.Data[0].(string))
			}
		})

		socket.On("leave-room", func(event *socketio.EventPayload) {
			socket.Leave("demo")
			io.To("demo").Emit("hello", socket.Id+" leave us room...", "server message")
		})

		socket.On("room-emit", func(event *socketio.EventPayload) {
			socket.To("demo").Emit("hello", socket.Id, event.Data)
		})

		socket.On("disconnecting", func(event *socketio.EventPayload) {
			println("disconnecting", socket.Nps, socket.Id)
		})

		socket.On("disconnect", func(event *socketio.EventPayload) {
			println("disconnect", socket.Nps, socket.Id)
		})
	})

	io.Of("/admin").OnConnection(func(socket *socketio.Socket) {
		println("connect", socket.Nps, socket.Id)
	})

	app.Use("/", io.Middleware)
	app.Route("/socket.io", io.Server)
}

func main() {
	app := fiber.New(fiber.Config{})

	app.Route("/", socketIoRoute)

	app.Get("/test", func(c *fiber.Ctx) error {
		io := c.Locals("io").(*socketio.Io)

		io.Emit("event", map[string]interface{}{
			"Ok": 1,
		})

		io.Of("/admin").Emit("event", map[string]interface{}{
			"Ok": 1,
		})

		return c.SendStatus(200)
	})

	app.Listen(":3000")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorInvalidConnection = errors.New("invalid connection")
	ErrorUUIDDuplication   = errors.New("UUID already exists")
)

Functions

This section is empty.

Types

type EventPayload

type EventPayload struct {
	Name   string //event name
	SID    string //socket id
	Socket *Socket
	Error  error
	Data   []interface{}
}

type Io

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

func New

func New() *Io

func (*Io) Close

func (s *Io) Close()

func (*Io) Emit

func (s *Io) Emit(event string, agrs ...interface{}) error

func (*Io) Middleware

func (s *Io) Middleware(c *fiber.Ctx) error

func (*Io) Of

func (s *Io) Of(name string) *Namespace

func (*Io) OnAuthorization

func (s *Io) OnAuthorization(fn func(params map[string]string) bool)

func (*Io) OnConnection

func (s *Io) OnConnection(fn connectionEventCallback)

func (*Io) Server

func (s *Io) Server(router fiber.Router)

func (*Io) To added in v0.1.4

func (s *Io) To(name string) *Room

type Namespace

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

func (*Namespace) Emit

func (nps *Namespace) Emit(event string, agrs ...interface{}) error

func (*Namespace) OnConnection

func (nps *Namespace) OnConnection(fn connectionEventCallback)

func (*Namespace) To added in v0.1.4

func (nps *Namespace) To(room string) *Room

type Room added in v0.1.4

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

func (*Room) Emit added in v0.1.4

func (room *Room) Emit(event string, agrs ...interface{}) error

type Socket

type Socket struct {
	Id   string
	Nps  string
	Conn *websocket.Conn

	Join  func(room string)
	Leave func(room string)
	To    func(room string) *Room
	// contains filtered or unexported fields
}

func (*Socket) Disconnect

func (s *Socket) Disconnect() error

func (*Socket) Emit

func (s *Socket) Emit(event string, agrs ...interface{}) error

func (*Socket) On

func (s *Socket) On(event string, fn eventCallback)

func (*Socket) Ping

func (s *Socket) Ping() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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