2

I have this object being sent to socket server in node-js, where client is trying to establish connection to the server & passing data for authentication. The object looks like this being passed.

const socket = io({
    auth: {
    userId: "",
    username:"",
   }
});
   

Now I'm trying to do the same in golang. I am using this library in golang to connect to server. https://github.com/hesh915/go-socket.io-client What would be the equivalent object in golang being sent as a map key-value pair with the following implementation.

opts := &socketio_client.Options{
    Transport: "websocket",
    Query:     make(map[string]string),
}
opts.Query["user"] = "user"
opts.Query["pwd"] = "pass"
uri := "http://192.168.1.70:9090/socket.io/"

client, err := socketio_client.NewClient(uri, opts)
if err != nil {
    log.Printf("NewClient error:%v\n", err)
    return
}

Tried to marshal structure of auth to byte [] and that type casted into string to opts.Query.

auth := &Auth{
    userId: "",
    username: "",
}

res, _ := json.Marshal(auth)
opts := &socketio_client.Options{
    Transport: "websocket",
    Query:     make(map[string]string),
}

opts.Query["auth"] = string(res)

But it throws an io.EOF error that's okay because the functionality isn't meant for auth. Of course query is a low level parameter in socket.io & auth isa socket level. Query option is meant for query params. Is there a way to pass auth params? Any clue how to achieve user authentication here?

Areej Fatimah
  • 251
  • 2
  • 11

0 Answers0