reactjssocketsgoglide-golang

I need solve a problem with websockets on golang server


I have a server on GoLang and googollee/go-socket.io.
When the server and the client work on the same port, the sockets work normally. But when I start they are on different ports an error occurs on the client:

WebSocket connection to 'ws://localhost:4444/socket.io/?EIO=3&transport=websocket&sid=6' failed: Error during WebSocket handshake: Unexpected response code: 403

POST http://localhost:4444/socket.io/?EIO=3&transport=polling&t=NDDzcYM&sid=5 400 (Bad Request)

GET http://localhost:4444/socket.io/?EIO=3&transport=polling&t=NDDzcYC&sid=5 400 (Bad Request)

WebSocket connection to 'ws://localhost:4444/socket.io/?EIO=3&transport=websocket&sid=5' failed: Error during WebSocket handshake: Unexpected response code: 403

And on the server:

connected: 1 closed client namespace disconnect meet error: json: cannot unmarshal object into Go value of type string connected: 2

This is my code:

package main

import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"

socketio "github.com/googollee/go-socket.io"
)

func main() {
fmt.Println("Hello world")
router := mux.NewRouter()
router.HandleFunc("/is-alive", isAlive).Methods("GET")

server, err := socketio.NewServer(nil)
if err != nil {
    log.Fatal(err)
}

server.OnConnect("/", func(s socketio.Conn) error {
    s.SetContext("")
    fmt.Println("connected:", s.ID())
    s.Emit("change")
    s.Join("bcast")
    return nil
})

server.OnEvent("/", "msg", func(s socketio.Conn, msg string) string {
    s.SetContext(msg)
    s.Emit("change")
    fmt.Println("event: msg")
    return "recv " + msg
})

server.OnError("/", func(s socketio.Conn, e error) {
    fmt.Println("meet error:", e)
})

server.OnDisconnect("/", func(s socketio.Conn, reason string) {
    fmt.Println("closed", reason)
})

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

router.Handle("/socket.io/", server)
c := cors.New(cors.Options{
    AllowedOrigins: []string{"http://localhost:3000"},
    AllowCredentials: true,
})
handler := c.Handler(router)

log.Println("Serving at localhost:4444...")
log.Fatal(http.ListenAndServe(":4444", handler))
}

func isAlive(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

On my react app

 import socketIOClient from "socket.io-client";

 export class SocketClientManager {

 public socket = socketIOClient("ws://localhost:4444");

 public subscribe() {
    this.socket.emit("msg", { msg: "Hello!" });
 }

 public setCallback(callback: (arg: any) => void) {
    this.socket.on("change", callback);
 }

 public async unsubscribe() {
    this.socket.disconnect();
 }

}


Solution

  • The error message indicates that the server tried to unmarshal your JSON object into a string, but it expected something different.

    The reason for that is in your method definition:

    server.OnEvent("/", "msg", func(s socketio.Conn, msg string) string {
    

    msg is of type string.

    However, then you send a message of a different type:

    public subscribe() {
        this.socket.emit("msg", { msg: "Hello!" });
    }
    

    This message could be described in this struct definition:

    type Message struct {
        Msg string `json:"msg"`
    }
    

    So unmarshaling your information into a string doesn't work because the object you sent is not a string, but must be represented different.

    To fix this, you have two options:

    1. Change your server-side method definition to be able to accept the client's data:

      Use server.OnEvent("/", "msg", func(s socketio.Conn, msg Message) string {, noting that msg is now of type Message, which is the struct that matches the data received.

    OR

    1. Change the data your client is sending:

      Use this.socket.emit("msg", "Hello!"); to send only a string (which will then be correctly decoded into your msg argument of type string.