zishang520/socket.io

Do you mind looking into Fiber adaptor for net/http interface to make this library work with fiber applications?

danielAsaboro opened this issue · 6 comments

here's the link: https://docs.gofiber.io/api/middleware/adaptor/

`go

package main

import (
"fmt"
"os"

"github.com/danielAsaboro/controllers"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/zishang520/engine.io/types"
"github.com/zishang520/socket.io/socket"

)

type Message struct {
Name string json:"name"
Message string json:"message"
}

func main() {
app := fiber.New()
var opts *socket.ServerOptions = socket.DefaultServerOptions()
var _ = &socket.ServerOptions{
// cors: {
// origin: "http://localhost:8100",
// methods: ["GET", "POST"],
// credentials: true
// },
// transports: ['websocket', 'polling'],
// allowEIO3: true

}
_ = &types.Cors{
	Origin:      "*",
	Methods:     []string{"GET", "POST"},
	Credentials: false,
}

// opts.SetCors(corsOptions)

app.Use(cors.New())
fmt.Print("printing addresses")
fmt.Println(opts)

io := socket.NewServer(nil, opts)
// io.of
// http.

app.Use("/socket.io/", adaptor.HTTPHandler(io.ServeHandler(opts)))
// http.Handle("/socket.io/", io.ServeHandler(nil))
// go http.ListenAndServe(":3000", nil)

io.On("connection", func(clients ...any) {
	client := clients[0].(*socket.Socket)
	client.On("event", func(datas ...any) {
	})
	client.On("disconnect", func(...any) {
	})
})
app.Get("/", controllers.HomeRouteHandler)

app.Listen(":" + os.Getenv("PORT"))

`

printing opts gave me nil all through...

&{{ 0xc0001703f0 } { } }

This is part of the plan and will be a refactored branch.

that would be much appreciated @zishang520 as this would allow me serve both rest and websocket on the same port
is there any way to help? would love to document as I can tell your schedule doesn't accommodate it

Thank you for your support. I will try to prioritize the implementation of this plan in the near future. After all, the plan has been on hold for some time. I am really sorry.

Sorry, due to technical issues, I cannot solve this problem: valyala/fasthttp#965. Currently, this requirement will be updated after fasthttp has a next step plan.

Currently, the submitted engine.io-server-go-fasthttp project and socket.io-server-go-fasthttp project cannot be used normally

New progress: Currently it can only be implemented based on the Websocket transport interface, and the Polling transport interface will be abandoned.

New research finds that it is now available, However, I also made a fasthttp related adaptation, but it only supports websocket: socket.io-server-go-fasthttp

Fasthttp has been updated not long ago (valyala/fasthttp@9c12719), and it was found to work properly in the test case:

package main

import (
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/adaptor"
	"github.com/zishang520/engine.io/v2/log"
	"github.com/zishang520/engine.io/v2/types"
	"github.com/zishang520/socket.io/v2/socket"
)

func main() {
	log.DEBUG = true
	c := socket.DefaultServerOptions()
	c.SetServeClient(true)
	// c.SetConnectionStateRecovery(&socket.ConnectionStateRecovery{})
	// c.SetAllowEIO3(true)
	c.SetPingInterval(300 * time.Millisecond)
	c.SetPingTimeout(200 * time.Millisecond)
	c.SetMaxHttpBufferSize(1000000)
	c.SetConnectTimeout(1000 * time.Millisecond)
	c.SetCors(&types.Cors{
		Origin:      "*",
		Credentials: true,
	})
	socketio := socket.NewServer(nil, nil)
	socketio.On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)

		client.On("message", func(args ...interface{}) {
			client.Emit("message-back", args...)
		})
		client.Emit("auth", client.Handshake().Auth)

		client.On("message-with-ack", func(args ...interface{}) {
			ack := args[len(args)-1].(func([]any, error))
			ack(args[:len(args)-1], nil)
		})
	})

	socketio.Of("/custom", nil).On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.Emit("auth", client.Handshake().Auth)
	})

	app := fiber.New()

	// app.Put("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c))) // test
	app.Get("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c)))
	app.Post("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c)))

	go app.Listen(":3000")

	exit := make(chan struct{})
	SignalC := make(chan os.Signal)

	signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range SignalC {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				close(exit)
				return
			}
		}
	}()

	<-exit
	socketio.Close(nil)
	os.Exit(0)
}