njones / socketio

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

Latest version at main branch doesn't allow clients to connect to multiple namespaces #60

Closed tavancini-tc closed 1 year ago

tavancini-tc commented 1 year ago

Latest version at the main branch (12ccfd1acb7b5610f89e980a3385bb1a0cd2a43b) doesn't allow clients to connect to multiple namespaces.

To reproduce:

Client (Python):

requirements.txt

python-socketio
python-socketio[client]==4.6.1

main.py

import socketio

NAMESPACES = ["/channel"]
URL = "http://127.0.0.1:8003/"

sio = socketio.Client(logger=True, engineio_logger=True)

@sio.event
def connect():
    print('connection established')

@sio.event
def connect_error(data):
    print("The connection failed!")

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect(URL,
    transports="websocket",
    namespaces=NAMESPACES)

sio.wait()

Server:

main.go

package main

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.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 namespace
    server.OnConnect(func(socket *sio.SocketV2) error {
        log.Printf("/ %s connected.", socket.ID().String())
        return nil
    })

    // Channel namespace
    channel := server.Of("/channel")
    channel.OnConnect(func(socket *sio.SocketV2) error {
        log.Printf("/channel %s connected.", socket.ID().String())
        return nil
    })

    log.Printf("Serving port %s...\n", port)
    log.Fatal(http.ListenAndServe(port, server))
}

If you use v0.1.1 -- go get github.com/njones/socketio@v0.1.1, it works, it connects to "/" and "/channel". But if you use the latest version -- go get github.com/njones/socketio@main, it doesn't work, it just connects to "/".

Let me know if you need anything else. Keep up the great work!

njones commented 1 year ago

Thanks for report, I'll look into this. If I have any more questions I'll ping this thread.

njones commented 1 year ago

Okay, I've found where the issue is. I should have a test and the fix out in about a day.

njones commented 1 year ago

This should be working, now. Thank you for the detailed bug reports. It helped in reproducing and fixing the bug.

tavancini-tc commented 1 year ago

Glad to help! I'll keep testing the lib in the next few days, if I find anything else I'll let you know. Thanks!