Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket-io: How to work with namespaces? #80

Open
sujit-baniya opened this issue Aug 29, 2023 · 0 comments
Open

socket-io: How to work with namespaces? #80

sujit-baniya opened this issue Aug 29, 2023 · 0 comments
Labels
question Further information is requested

Comments

@sujit-baniya
Copy link

Hello, I'm trying to use this package for different namespaces

Here's the working version using github.com/googollee/go-socket.io

func main() {
	server := socketio.NewServer(&engineio.Options{
		Transports: []transport.Transport{
			&polling.Transport{
				CheckOrigin: allowOriginFunc,
			},
			&websocket.Transport{
				CheckOrigin: allowOriginFunc,
			},
		},
	})
	
	server.OnConnect("/", func(s socketio.Conn) error {
		s.SetContext("")
		log.Println("connected:", s.ID())
		return nil
	})
	
	server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
		log.Println("notice:", msg)
		s.Emit("reply", "have "+msg)
	})
	
	server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
		log.Println("chat:", msg)
		s.SetContext(msg)
		return "recv " + msg
	})
	
	server.OnEvent("/", "bye", func(s socketio.Conn) string {
		last := s.Context().(string)
		s.Emit("bye", last)
		s.Close()
		return last
	})
	
	server.OnError("/", func(s socketio.Conn, e error) {
		log.Println("meet error:", e)
	})
	
	server.OnDisconnect("/", func(s socketio.Conn, reason string) {
		log.Println("closed", reason)
	})
	
	go func() {
		if err := server.Serve(); err != nil {
			log.Fatalf("socketio listen error: %s\n", err)
		}
	}()
	defer server.Close()
	
	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("asset")))
	
	log.Println("Serving at http://localhost:8001...")
	log.Fatal(http.ListenAndServe(":8001", nil))
}

When I try to use this package I'm getting error. Not sure what might be the issue

func main() {
	port := ":8001"
	
	server := sio.NewServer(
		eio.WithPingInterval(300*1*time.Millisecond),
		eio.WithPingTimeout(200*1*time.Millisecond),
		eio.WithMaxPayload(1000000),
		eio.WithTransportOption(eiot.WithGovernor(1500*time.Microsecond, 500*time.Microsecond)),
	)
	
	server.OnConnect(func(s *sio.SocketV4) error {
		log.Println("connected:", s.ID())
		s.Of("/").On("notice", CustomWrap(func(a string) error {
			return s.Emit("reply", seri.String("have "+a))
		}))
		s.Of("/").On("bye", CustomWrap(func(a string) error {
			return s.Emit("bye", seri.String(a))
		}))
		s.Of("/chat").On("msg", CustomWrap(func(a string) error {
			fmt.Println("msg", a)
			return nil
		}))
		return nil
	})
	
	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("asset")))
	log.Printf("serving port %s...\n", port)
	log.Fatal(http.ListenAndServe(port, nil))
}

// Define a custom wrapper 
type CustomWrap func(string) error

// Define your callback
func (cc CustomWrap) Callback(data ...interface{}) error {
	a, aOK := data[0].(string)
	
	if !aOK {
		return fmt.Errorf("bad parameters")
	}
	
	return cc(a)
}

Error:

44{"message":"expected an []interface{} or []string, found []interface {}\t{"do":"eventPacket"}"}

index.html

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io("http://127.0.0.1:8001", {transports: ["websocket"]});

      socket.on('reply', function(msg){
        $('#messages').append($('<li>').text(msg));
      });

      $('form').submit(function(){
        socket.emit('msg', $('#m').val(), function(data){
          $('#messages').append($('<li>').text('ACK CALLBACK: ' + data));
        });

        socket.emit('notice', $('#m').val());

        $('#m').val('');
        return false;
      });
    </script>
  </body>
</html>
@njones njones added the question Further information is requested label Jan 23, 2024
@njones njones changed the title [QUESTION] - How to work with namespace socket-io: How to work with namespace Jan 23, 2024
@njones njones changed the title socket-io: How to work with namespace socket-io: How to work with namespaces? Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants