title: Go & Socket.io HTTP + WSS on one port with CORS?
body: <p>Brand new to Go.. Still obviously learning the syntax and the basics.. But I do have a specific goal in mind..</p>
  
  <p>I'm trying to just get a simple server up on :8080 that can respond to both HTTP and socket.io (via <code>/socket.io/</code> url), specificaly with CORS.</p>
  
  <p>My code:</p>
  
  <pre><code>package main
  
  import (
       "log"         
       "net/http"
       "github.com/rs/cors"
       "github.com/googollee/go-socket.io"
   )
  
   func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
       w.Write([]byte("Hello, World!"))
   }
  
   func main() {
  
     c := cors.New(cors.Options{
           AllowedOrigins: []string{"*"},
           AllowCredentials: true,
     })
  
     server, err := socketio.NewServer(nil)
       if err != nil {
           log.Fatal(err)
       }
  
  
     server.On("connection", func(so socketio.Socket) {
        log.Println("on connection")
        so.Join("chat")
        so.On("chat message", func(msg string) {
            log.Println("emit:", so.Emit("chat message", msg))
            so.BroadcastTo("chat", "chat message", msg)
        })
        so.On("disconnection", func() {
            log.Println("on disconnect")
        })
     })
  
     server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
     })    
  
  
     http.Handle("/socket.io/", c.Handler(server))
     http.HandleFunc("/", SayHelloWorld)
     log.Println("Serving at localhost:8080...")
     log.Fatal(http.ListenAndServe(":8080", nil))
  }
  </code></pre>
  
  <p>On the client side I'm still seeing:</p>
  
  <blockquote>
    <p>WebSocket connection to 'wss://api.domain.com/socket.io/?EIO=3&amp;transport=websocket&amp;sid=xNWd9aZvwDnZOrXkOBaC' failed: WebSocket is closed before the connection is established.</p>
    
    <p>(index):1 XMLHttpRequest cannot load <a href="https://api.domain.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1420662449235-3932&amp;sid=xNWd9aZvwDnZOrXkOBaC" rel="nofollow">https://api.domain.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1420662449235-3932&amp;sid=xNWd9aZvwDnZOrXkOBaC</a>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://fiddle.jshell.net" rel="nofollow">http://fiddle.jshell.net</a>' is therefore not allowed access.</p>
  </blockquote>
  
  <p><strong><em>EDIT #1:</em></strong></p>
  
  <p>So I've been banging my head away trying to understand why I can't connect.. Came across an even more confusing piece of the puzzle?</p>
  
  <p><a href="https://gist.github.com/acoyfellow/167b055da85248c94fc4" rel="nofollow">https://gist.github.com/acoyfellow/167b055da85248c94fc4</a></p>
  
  <p>The above gist is the code of my golang server + the browser code used to connect.. This code will send 30 HTTP GET requests <strong>per second</strong> to the backend, without connecting, upgrading, or giving any errors (client or server side).. it essentially DDOS's my own backend?</p>
  
  <p>Someone, please someone tell me I'm doing something stupid.. This is quite the pickle :P</p>
  
  <p><strong><em>EDIT #2:</em></strong></p>
  
  <p>I can stop the "DDOS" by simply adjusting the trailing <code>/</code> on the URL of the socket.io endpoint in Go.. So: <code>mux.Handle("/socket.io", server)</code> to <code>mux.Handle("/socket.io/", server)</code> will now produce error messages and connection attempts with error responses of:</p>
  
  <blockquote>
    <p>WebSocket connection to 'wss://api.domain.com/socket.io/?EIO=3&amp;transport=websocket&amp;sid=0TzmTM_QtF1TaS4exiwF' failed: Error during WebSocket handshake: Unexpected response code: 400 socket.io-1.2.1.js:2 </p>
    
    <p>GET <a href="https://api.domain.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1420743204485-62&amp;sid=0TzmTM_QtF1TaS4exiwF" rel="nofollow">https://api.domain.com/socket.io/?EIO=3&amp;transport=polling&amp;t=1420743204485-62&amp;sid=0TzmTM_QtF1TaS4exiwF</a> 400 (Bad Request)</p>
  </blockquote>