Comments (6)
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.
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.
+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.
Max-Liu, is the customServer something like this?
type customServer struct {
Server *socketio.Server
}
from go-socket.io.
@Max-Liu Yes it is, thanks. It works ;)
from go-socket.io.
@Max-Liu Did u maybe managed to get socket.io to work with gin ?
from go-socket.io.
Related Issues (20)
- v1.6.1 or above sends events in the OnConnect() method, but the client cannot receive the message.
- v1.6.1 or above sends events in the OnConnect() method, but the client cannot receive the message.
- Cors problem with Golang Server HOT 2
- How to use middlewares to authentication? HOT 2
- Additional bytes in HTTP handshake HOT 1
- server.Remove undefined HOT 3
- golang 客户端包 如何使用命名空间呢 默认都是 根 / 修改不了
- how to send binary message? HOT 1
- Pingtimeout and pinginterval does not work
- No NewClient method HOT 1
- Client connect root ('/') doesn't have a namespace handler HOT 6
- how to support socket.ioV3 HOT 1
- how to reconnect when disconnected ?
- Client ignores Transports defined in opts HOT 3
- Support "extraHeaders" in client HOT 1
- read tcp 127.0.0.1:8001->127.0.0.1:55169: i/o timeout
- Very bad source HOT 1
- [Feature] Emit to a Specific Client Only HOT 6
- How to receive Binary data
- When I frequently push data to the server, it becomes like this.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-socket.io.