go-socket.io icon indicating copy to clipboard operation
go-socket.io copied to clipboard

OnEvent in same nps with OnConnection dont work

Open lucasres opened this issue 5 years ago • 2 comments

Describe the bug I try make a simple chat following the documentation but does not work. I make main.go with OnConnection and OnEvent sames in /socket.io/ nsp. But when my client(index.html bellow) emit evento to msg. But nothing happenes. Should print message in log terminal

To Reproduce main.go

package main

import (
	socketio "github.com/googollee/go-socket.io"
	"log"
	"net/http"
)

func main() {
	//instancia o servidor
	server, err := socketio.NewServer(nil)
	if err != nil {
		log.Println(err)
	}

	server.OnConnect("/", func(so socketio.Conn) error {
		log.Println(so.ID())
		return nil
	})

	server.OnEvent("/", "msg", func(so socketio.Conn) error {
		log.Println("message")
		return nil
	})

	go server.Serve()
	defer server.Close()

	http.Handle("/socket.io/", server)
	http.Handle("/", http.FileServer(http.Dir("./asset")))
	log.Println("Start server in 8000...")
	log.Fatal(http.ListenAndServe(":8000", nil))
}

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  hello world
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>
  const socket = io("http://localhost:8000/socket.io/");
  socket.emit("msg","hello world");
</script>

</html>

Expected behavior Print in terminal message

Environment:

  • Go version: go version go1.13.3 linux/amd64
  • Server version: go-socket.io 1.4
  • Client version socket.io 2.1.1

lucasres avatar May 23 '20 01:05 lucasres

As far as I know, the connection mode of socket client has changed let socket = io("http://127.0.0.1:8000",{ path:"/socket.io" })

LinDejiang avatar Jun 23 '20 09:06 LinDejiang

Don't you need to wait for the connection to be made before emiting?

erkie avatar Sep 16 '20 07:09 erkie