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

Client Require []string in handler instead of string

Open Speshl opened this issue 2 years ago • 3 comments

Describe the bug When using the go-socket.io client, the client side handlers have to be []string instead of string as mentioned in the example.

Note: the client also seems to require atleast one on event listener to work. Will not start with no listeners (if trying to only send and not recieve)

To Reproduce

//client side
a.client.OnEvent("register_success", a.onRegisterSuccess)
err := a.client.Connect() //Client must have atleast 1 event handler to work
if err != nil {
    return fmt.Errorf("error connecting to server - %w", err)
}
func (a *App) onRegisterSuccess(socketConn socketio.Conn, msg []string) {
	log.Printf("server registration success: %+v\n", msg)
}

//server side
s.CarConns.emit(socketConn.ID(), "register_success", "success")

func (c *Connections) emit(id string, event string, msg ...interface{}) {
	c.lock.RLock()
	defer c.lock.RUnlock()
	_, ok := c.connections[id]
	if ok {
		log.Printf("Emitting %s@%s\n", id, event)
		c.connections[id].Socket.Emit(event, msg)
	}
}

Expected behavior Expected to have handler be string instead of []string

Environment (please complete the following information):

  • Go version: go1.20.6 windows/amd64
  • Server version: v1.8.0-rc.1
  • Client version: v1.8.0-rc.1

Additional context Here is the error message received when attempting to use just a string in the handler as the example suggests:

2023/09/20 22:13:41 ERROR clientError err="error in namespace: () with error: (json: cannot unmarshal array into Go value of type string)"
2023/09/20 22:13:41 INFO Error decoding the message type namespace="" event=register_success eventType=[string] err="json: cannot unmarshal array into Go value of type string"
2023/09/20 22:13:41 ERROR client read: err="decode args error"
2023/09/20 22:13:41 INFO clientWrite Writer loop has stopped

Speshl avatar Sep 21 '23 03:09 Speshl

any update on this?

justvisiting avatar Feb 07 '24 01:02 justvisiting

Hey, @justvisiting Why are you wait updates? Open source it is not development consulting

sshaplygin avatar Feb 07 '24 10:02 sshaplygin

maybe you can add a type struct in your OnEvent msg [TypeStruct]

type Authenticate struct {
	Id       string `json:"id"`
	Username string `json:"username"`
}

type SocketMessage struct {
	Event string      `json:"event"`
	Data  interface{} `json:"data"`
}
client.OnEvent("message", func(s socketio.Conn, msg SocketMessage ) {
    log.Println("Receive Message: ", "message", msg.Event)
})

it can deal with error: "with error: (json: cannot unmarshal array into Go value of type string)"

lettered avatar Mar 13 '24 06:03 lettered

archived.

googollee avatar Sep 29 '24 15:09 googollee