Use new Upgrader API in examples

This commit is contained in:
Joachim Bauch 2014-04-16 17:42:15 +02:00
parent b2f990180a
commit 9474333b60
3 changed files with 30 additions and 27 deletions

View File

@ -28,7 +28,10 @@ import (
// echoCopy echoes messages from the client using io.Copy. // echoCopy echoes messages from the client using io.Copy.
func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) { func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
conn, err := websocket.Upgrade(w, r, nil, 4096, 4096) u := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096}
conn, err := u.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Println("Upgrade:", err) log.Println("Upgrade:", err)
http.Error(w, "Bad request", 400) http.Error(w, "Bad request", 400)
@ -87,10 +90,12 @@ func echoCopyFull(w http.ResponseWriter, r *http.Request) {
// echoReadAll echoes messages from the client by reading the entire message // echoReadAll echoes messages from the client by reading the entire message
// with ioutil.ReadAll. // with ioutil.ReadAll.
func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage bool) { func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage bool) {
conn, err := websocket.Upgrade(w, r, nil, 4096, 4096) u := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096}
conn, err := u.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Println("Upgrade:", err) log.Println("Upgrade:", err)
http.Error(w, "Bad request", 400)
return return
} }
defer conn.Close() defer conn.Close()

View File

@ -89,16 +89,17 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", 405) http.Error(w, "Method not allowed", 405)
return return
} }
if r.Header.Get("Origin") != "http://"+r.Host { u := websocket.Upgrader{
http.Error(w, "Origin not allowed", 403) ReadBufferSize: 1024,
return WriteBufferSize: 1024,
} CheckOrigin: func(r *http.Request) bool {
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) return r.Header.Get("Origin") == "http://"+r.Host
if _, ok := err.(websocket.HandshakeError); ok { }}
http.Error(w, "Not a websocket handshake", 400) ws, err := u.Upgrade(w, r, nil)
return if err != nil {
} else if err != nil { if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err) log.Println(err)
}
return return
} }
c := &connection{send: make(chan []byte, 256), ws: ws} c := &connection{send: make(chan []byte, 256), ws: ws}

View File

@ -107,20 +107,17 @@ func writer(ws *websocket.Conn, lastMod time.Time) {
} }
func serveWs(w http.ResponseWriter, r *http.Request) { func serveWs(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" { u := websocket.Upgrader{
http.Error(w, "Method not allowed", 405) ReadBufferSize: 1024,
return WriteBufferSize: 1024,
} CheckOrigin: func(r *http.Request) bool {
if r.Header.Get("Origin") != "http://"+r.Host { return r.Header.Get("Origin") == "http://"+r.Host
http.Error(w, "Origin not allowed", 403) }}
return ws, err := u.Upgrade(w, r, nil)
} if err != nil {
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) if _, ok := err.(websocket.HandshakeError); !ok {
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
} else if err != nil {
log.Println(err) log.Println(err)
}
return return
} }