antoniodipinto / ikisocket

🧬 WebSocket wrapper with event management for Fiber https://github.com/gofiber/fiber. Based on Fiber WebSocket and inspired by Socket.io
MIT License
123 stars 21 forks source link

Unexpected event-handler invoking #27

Closed hoangtuan151 closed 2 years ago

hoangtuan151 commented 2 years ago

Describe the bug Firstly I want to say thx for your awesome work! My issue is that: in the same code base, I write 2 socket servers for different purposes. From wscat client, I connect to server A and then disconnect it and both disconnect event-handlers of 2 servers got invoked.

To Reproduce Steps to reproduce the behavior:

  1. Codes

main.go

package main

func main() {
    s1 := CustomerServer{}
    go func() {
        s1.ListenAndServe(":5001")
    }()

    s2 := ProductServer{}
    go func() {
        s2.ListenAndServe(":5002")
    }()

    select {}
}

customer_server.go

package main

import (
    "log"

    "github.com/antoniodipinto/ikisocket"
    "github.com/gofiber/fiber/v2"
)

type CustomerServer struct{}

func (s CustomerServer) ListenAndServe(addr string) error {
    app := fiber.New()

    app.Get("/customer/:id", ikisocket.New(custHandleNewConn))

    ikisocket.On(ikisocket.EventDisconnect, custHandleDisconnect)

    return app.Listen(addr)
}

func custHandleNewConn(kws *ikisocket.Websocket) {
    log.Printf("[Customer Server] got new connection! %#v, %#v\n", kws.GetUUID(), kws.Params("id"))
    kws.SetAttribute("test_id", kws.Params("id"))
}

func custHandleDisconnect(ep *ikisocket.EventPayload) {
    log.Printf("[Customer Server] disconnect from %#v\n", ep.SocketAttributes["test_id"])
}

product_server.go

package main

import (
    "log"

    "github.com/antoniodipinto/ikisocket"
    "github.com/gofiber/fiber/v2"
)

type ProductServer struct{}

func (s ProductServer) ListenAndServe(addr string) error {
    app := fiber.New()

    app.Get("/product/:id", ikisocket.New(productHandleNewConn))

    ikisocket.On(ikisocket.EventDisconnect, productHandleDisconnect)

    return app.Listen(addr)
}

func productHandleNewConn(kws *ikisocket.Websocket) {
    log.Printf("[Product Server] got new connection! %#v, %#v\n", kws.GetUUID(), kws.Params("id"))
    kws.SetAttribute("test_id", kws.Params("id"))
}

func productHandleDisconnect(ep *ikisocket.EventPayload) {
    log.Printf("[Product Server] disconnect from %#v\n", ep.SocketAttributes["test_id"])
}
  1. Attach log

starting servers

➜  chat-server go run .

 ┌───────────────────────────────────────────────────┐ 
 │                   Fiber v2.37.1                   │ 
 │               http://127.0.0.1:5002               │ 
 │       (bound on host 0.0.0.0 and port 5002)       │ 
 │                                                   │ 
 │ Handlers ............. 2  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ........... 1869661 │ 
 └───────────────────────────────────────────────────┘ 

 ┌───────────────────────────────────────────────────┐ 
 │                   Fiber v2.37.1                   │ 
 │               http://127.0.0.1:5001               │ 
 │       (bound on host 0.0.0.0 and port 5001)       │ 
 │                                                   │ 
 │ Handlers ............. 2  Processes ........... 1 │ 
 │ Prefork ....... Disabled  PID ........... 1869661 │ 
 └───────────────────────────────────────────────────┘ 

2022/09/19 12:15:05 [Customer Server] got new connection! "ONjHvu2ivwdHqiqJn55yTPMAMEEOyPVJ9UV9GwfeGxSQSVIA6mNAT8z31h35Pe0ZnjmGvGAMVFFRObxB0g4Gy0tZHdv8hqChwTTS", "cust_id_1"
2022/09/19 12:15:08 [Product Server] disconnect from "cust_id_1"
2022/09/19 12:15:08 [Customer Server] disconnect from **"cust_id_1"**

wscat client-side

➜  ~ wscat -c ws://localhost:5001/customer/cust_id_1
Connected (press CTRL+C to quit)
> % 
  1. Define expected behavior

Only event-handler of relevant server will be called.

antoniodipinto commented 2 years ago

Hi @hoangtuan151,

I'm sorry but at the moment Ikisocket only supports single instance. Wich means you can only use for a standard behavior of, one fiber instance per app. Not multiple fiber instances per app

hoangtuan151 commented 2 years ago

Thx for your quick response @antoniodipinto , I think it would be a huge improvement ;) looking forward to it...