njones / socketio

A Modern SocketIO library for go
MIT License
60 stars 9 forks source link

WithPath option is not working #70

Closed tavancini-tc closed 1 year ago

tavancini-tc commented 1 year ago

When defining a specific path on the server and the client, the connection is not established by the client.

Node client:

const io = require("socket.io-client");

const socket = io("http://localhost:8003/channel", {
    transports: ["websocket"],
    reconnection: true,
    autoConnect: true,
    pingInterval: 8000,
    pingTimeout: 10000,
    connectTimeout: 10000,
    query: {
        token: "token!",
    },
    path: "/testing/",
});

socket.on("connect", () => {
    console.log("Connected.");
});

socket.on("message", (msg) => {
    console.log("Message: ", msg)
})

socket.on("error", (error) => {
    console.log("Error: ", error);
});

socket.on("disconnect", (error) => {
    console.log("Disconnected: ", error);
});

socket.on("connect_error", (error) => {
    console.log("connect_error: ", error);
});

socket.connect();

Go server:

import (
    "log"
    "net/http"
    "time"

    sio "github.com/njones/socketio"
    eio "github.com/njones/socketio/engineio"
    eiot "github.com/njones/socketio/engineio/transport"
)

func main() {
    port := "localhost:8003"

    server := sio.NewServerV2(
                eio.WithPath("/testing/"),
        eio.WithSessionShave(1*time.Millisecond),
        eio.WithPingInterval(5*time.Second),
        eio.WithPingTimeout(1*time.Minute),
        eio.WithMaxPayload(1000000),
        eio.WithTransportOption(eiot.WithGovernor(1500*time.Microsecond, 500*time.Microsecond)),
    )

    //////////////////////////////////////////////////
    // ROOT
    server.OnConnect(func(socket *sio.SocketV2) error {
        log.Printf("/ %s connected.", socket.ID().String())
        log.Printf("/ token: %s", socket.Request().URL.Query().Get("token"))
        return nil
    })

    //////////////////////////////////////////////////
    // CHANNEL
    channel := server.Of("/channel")
    channel.OnConnect(func(socket *sio.SocketV2) error {
        log.Printf("/channel %s connected.", socket.ID().String())
        log.Printf("/channel token: %s", socket.Request().URL.Query().Get("token"))
        return nil
    })

    //////////////////////////////////////////////////
    log.Printf("Serving port %s...\n", port)
    log.Fatal(http.ListenAndServe(port, server))
}
njones commented 1 year ago

I'm looking into this, hopefully I can roll this fix in to the next patch release that I plan to finalize this weekend.

Thanks for submitting this bug report (again with the great code examples to follow)!

tavancini-tc commented 1 year ago

Hey Nika, do you have any updates? Thanks!

njones commented 1 year ago

The week got a bit more busier than expected, and I had to do a few refactors of the code that I was working on. But I will absolutely try and get this code out and committed by the end of this weekend. Sorry for the delay, and thanks for your patience.

tavancini-tc commented 1 year ago

Np, thanks!

njones commented 1 year ago

Hi @tavancini-tc,

I added the socketio.WithPath option that you can use with your code. That should work if you update the server config to be something like:

server := sio.NewServerV2(
    sio.WithPath("/testing/"), // NOTE: the 'sio.' here.
    eio.WithSessionShave(1*time.Millisecond),
    eio.WithPingInterval(5*time.Second),
    eio.WithPingTimeout(1*time.Minute),
    eio.WithMaxPayload(1000000),
    eio.WithTransportOption(eiot.WithGovernor(1500*time.Microsecond, 500*time.Microsecond)),
)
tavancini-tc commented 1 year ago

Great, thanks! I'll run some tests and see if everything is ok.