Git Product home page Git Product logo

Comments (6)

Max-Liu avatar Max-Liu commented on June 12, 2024 4

you can overwrite ServeHTTP method with your custom struct.

func (s *customServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Credentials", "true")
    origin := r.Header.Get("Origin")
    w.Header().Set("Access-Control-Allow-Origin", origin)
    s.Server.ServeHTTP(w, r)
}

from go-socket.io.

dpastoor avatar dpastoor commented on June 12, 2024 4

for anyone that stumbles on this issue again and is stuck, here is the code I was able to use

package main

import (
    "log"
    "net/http"

    "github.com/googollee/go-socket.io"
    "github.com/rs/cors"
)

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.Write([]byte("{\"hello\": \"world\"}"))
    })

    // cors.Default() setup the middleware with default options being
    // all origins accepted with simple methods (GET, POST). See
    // documentation below for more options.
    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.On("connection", func(so socketio.Socket) {
        so.Emit("chat", "hello message")
        log.Println("on connection")
        so.On("chat", func(msg string) {
            log.Println("recieved message", msg)
        })
        // Socket.io acknowledgement example
        // The return type may vary depending on whether you will return
        // For this example it is "string" type
        so.On("chat message with ack", func(msg string) string {
            return msg
        })
        so.On("disconnection", func() {
            log.Println("disconnected from chat")
        })
    })
    server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
    })

    mux.Handle("/socket.io/", server)
    mux.Handle("/assets", http.FileServer(http.Dir("./assets")))

       // provide default cors to the mux
    handler := cors.Default().Handler(mux)

c := cors.New(cors.Options{
        AllowedOrigins:   []string{"*"},
        AllowCredentials: true,
    })

    // decorate existing handler with cors functionality set in c
    handler = c.Handler(handler)

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

from go-socket.io.

acoyfellow avatar acoyfellow commented on June 12, 2024

+1 I can't seem to figure out how to set CORS for socket.io connections
edit: http://stackoverflow.com/questions/27828052/go-socket-io-http-wss-on-one-port-with-cors

from go-socket.io.

Netherdrake avatar Netherdrake commented on June 12, 2024

Max-Liu, is the customServer something like this?

type customServer struct {
  Server *socketio.Server
}

from go-socket.io.

Netherdrake avatar Netherdrake commented on June 12, 2024

@Max-Liu Yes it is, thanks. It works ;)

from go-socket.io.

ascepanovic avatar ascepanovic commented on June 12, 2024

@Max-Liu Did u maybe managed to get socket.io to work with gin ?

from go-socket.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.