title: Serving a websocket in Go
body: <p>I'm starting to play around with websockets + go and well I think I'm misunderstanding something quite basic with websockets in Go.</p>
  
  <p>I'd like to simply listen for a websocket connection and process accordingly. However all examples I see in Go using websocket is serving the web page that then connects to the websocket, is this a requirement?</p>
  
  <p>The following is a basic echo server I have setup:</p>
  
  <pre><code>package main
  
  import (
    "fmt"
    "code.google.com/p/go.net/websocket"
    "net/http"
  )
  
  func webHandler(ws *websocket.Conn) {
    var s string
    fmt.Fscan(ws, &amp;s)
    fmt.Println("Received: ", s)
  }
  
  func main() {
    fmt.Println("Starting websock server: ")
    http.Handle("/echo", websocket.Handler(webHandler))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
      panic("ListenAndServe: " + err.Error())
    }
  }
  </code></pre>
  
  <p>This is the javascript used to connect: </p>
  
  <pre><code>ws = new WebSocket("ws://localhost:8080/echo");
  ws.onmessage = function(e) {
      console.log("websock: " + e.data);
  };
  </code></pre>
  
  <p>However this results in:
     WebSocket connection to 'ws://localhost:8080/echo' failed: Unexpected response code: 403 </p>