Skip to content

Instantly share code, notes, and snippets.

@joemccall86
Forked from edrex/Caddyfile
Last active March 6, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemccall86/15ef4e2610dc193ffcf1 to your computer and use it in GitHub Desktop.
Save joemccall86/15ef4e2610dc193ffcf1 to your computer and use it in GitHub Desktop.

Failing websocket proxy test case.

  • $(1) go run wsecho.go listens on :8080
  • $(2) caddy
  • open http://localhost:2015
  • note 404 response to websocket request
localhost
log access.log
proxy /echo 127.0.0.1:8080 {
websocket
# treat the path as if it were sent without the /echo
without /echo
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
</head>
<body>
<script>
function testEchoSocket(path) {
var s = new WebSocket('ws://'+location.hostname+':'+location.port + path);
s.onmessage = function (event) {
console.log(event.data);
}
s.onopen = function (event) {
s.send("ohai");
}
}
testEchoSocket('/echo');
</script>
</body>
</html>
package main
import (
"fmt"
"io"
"net/http"
"golang.org/x/net/websocket"
)
func echoHandler(ws *websocket.Conn) {
fmt.Println("echoHandler")
io.Copy(ws, ws)
}
func main() {
http.Handle("/echo", websocket.Handler(echoHandler))
http.Handle("/", websocket.Handler(echoHandler))
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
fmt.Println(status, r.URL.Path)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
errorHandler(w, r, http.StatusNotFound)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment