echo_socket_io

package module
v0.0.0-...-1f52ed4 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2020 License: MIT Imports: 4 Imported by: 1

README

Wrapper for use Echo context with Socket.io.

Build Status

Documentation

pkg.go.dev

Install

Install the package with:

go get -u github.com/umirode/echo-socket.io

Import it with:

import esi "github.com/umirode/echo-socket.io"

and use esi inside your code.

Example

package main

import (
	"fmt"
	socketio "github.com/googollee/go-socket.io"
	"github.com/labstack/echo/v4"
	esi "github.com/umirode/echo-socket.io"
)

func main() {
	e := echo.New()

	e.Any("/socket.io/", socketIOWrapper())

	e.Logger.Fatal(e.Start(":8080"))
}

func socketIOWrapper() func(context echo.Context) error {
	wrapper, err := esi.NewWrapper(nil)
	if err != nil {
		fmt.Println(err.Error())
		return nil
	}

	wrapper.OnConnect("", func(context echo.Context, conn socketio.Conn) error {
		conn.SetContext("")
		fmt.Println("connected:", conn.ID())
		return nil
	})
	wrapper.OnError("", func(context echo.Context, e error) {
		fmt.Println("meet error:", e)
	})
	wrapper.OnDisconnect("", func(context echo.Context, conn socketio.Conn, msg string) {
		fmt.Println("closed", msg)
	})

	wrapper.OnEvent("", "test", func(context echo.Context, conn socketio.Conn, msg string) {
		conn.SetContext(msg)
		fmt.Println("notice:", msg)
		conn.Emit("test", msg)
	})

	return wrapper.HandlerFunc
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IWrapper

type IWrapper interface {
	OnConnect(nsp string, f func(echo.Context, socketio.Conn) error)
	OnDisconnect(nsp string, f func(echo.Context, socketio.Conn, string))
	OnError(nsp string, f func(echo.Context, error))
	OnEvent(nsp, event string, f func(echo.Context, socketio.Conn, string))
	HandlerFunc(context echo.Context) error
}

Socket.io wrapper interface

type Wrapper

type Wrapper struct {
	Context echo.Context
	Server  *socketio.Server
}

func NewWrapper

func NewWrapper(options *engineio.Options) (*Wrapper, error)

Create wrapper and Socket.io server

func NewWrapperWithServer

func NewWrapperWithServer(server *socketio.Server) (*Wrapper, error)

Create wrapper with exists Socket.io server

func (*Wrapper) HandlerFunc

func (s *Wrapper) HandlerFunc(context echo.Context) error

Handler function

func (*Wrapper) OnConnect

func (s *Wrapper) OnConnect(nsp string, f func(echo.Context, socketio.Conn) error)

On Socket.io client connect

func (*Wrapper) OnDisconnect

func (s *Wrapper) OnDisconnect(nsp string, f func(echo.Context, socketio.Conn, string))

On Socket.io client disconnect

func (*Wrapper) OnError

func (s *Wrapper) OnError(nsp string, f func(echo.Context, error))

On Socket.io error

func (*Wrapper) OnEvent

func (s *Wrapper) OnEvent(nsp, event string, f func(echo.Context, socketio.Conn, string))

On Socket.io event from client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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