Skip to content

Instantly share code, notes, and snippets.

@igm
Last active August 29, 2015 14:01
Show Gist options
  • Save igm/af30896a2e97f17125ac to your computer and use it in GitHub Desktop.
Save igm/af30896a2e97f17125ac to your computer and use it in GitHub Desktop.
Websocket problem
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
"time"
"github.com/gorilla/websocket"
)
var (
port = os.Getenv("VCAP_APP_PORT")
)
func main() {
if port == "" {
port = "8080"
}
http.HandleFunc("/", indexHandler)
http.HandleFunc("/websocket", wsHandler)
http.ListenAndServe(":"+port, nil)
}
func wsHandler(rw http.ResponseWriter, req *http.Request) {
dump, _ := httputil.DumpRequest(req, true)
log.Println(string(dump))
if req.Header.Get("Origin") != "http://"+req.Host {
http.Error(rw, "Origin not allowed", 403)
return
}
conn, err := websocket.Upgrade(rw, req, nil, 1024, 1024)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
for {
if err := conn.WriteJSON(time.Now()); err != nil {
fmt.Println(err)
return
}
time.Sleep(1000 * time.Millisecond)
}
}
func indexHandler(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(html))
}
var html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simpe Websocket Test</title>
</head>
<body>
<h1>Hello</h1>
<textarea rows=20 cols=80 id="output"></textarea>
<script>
websocket = new WebSocket("ws://"+window.location.host+"/websocket");
websocket.onopen = function(evt) { console.log("open"); };
websocket.onclose = function(evt) { console.log("close"); };
websocket.onmessage = function(evt) { document.getElementById("output").value += evt.data; };
websocket.onerror = function(evt) { console.log(evt); };
</script>
</body>
</html>
`
/*
Request dumps:
-------------
localhost:
GET /websocket HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
Connection: Upgrade
Origin: http://localhost:8080
Pragma: no-cache
Sec-Websocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
Sec-Websocket-Key: L6xw1OdN7oYLOAMzbYNGwA==
Sec-Websocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36
PWS:
----
GET /websocket HTTP/1.1
Host: cfws.cfapps.io
Accept-Encoding: gzip
Cache-Control: no-cache
Cookie: _ga=GA1.2.910257418.1377268645; _sm_au_c=iVV22fn1sFH6T1Pt0e
Origin: http://cfws.cfapps.io
Pragma: no-cache
Sec-Websocket-Extensions: permessage-deflate; client_max_window_bits, x-webkit-deflate-frame
Sec-Websocket-Key: nlxia5iSyEaEMVJ7XsWYOQ==
Sec-Websocket-Version: 13
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36
X-Forwarded-For: 95.172.74.46, 10.10.2.122
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Request-Start: 1400072665355
X-Vcap-Request-Id: 2b37bc59-171a-4061-43dc-257bef82f16a
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment