Skip to content

Instantly share code, notes, and snippets.

@hariharan-uno
Created March 21, 2014 19:46
Show Gist options
  • Save hariharan-uno/9694696 to your computer and use it in GitHub Desktop.
Save hariharan-uno/9694696 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/gorilla/websocket"
"io"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
} else if err != nil {
log.Println(err)
return
}
for {
messageType, r, err := ws.NextReader()
if err != nil {
log.Println(err)
return
}
w, err := ws.NextWriter(messageType)
if err != nil {
log.Println(err)
return
}
if _, err := io.Copy(w, r); err != nil {
log.Println(err)
return
}
if err := w.Close(); err != nil {
log.Println(err)
return
}
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":6060", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment