Moved Upgrader variable to package level.

This commit is contained in:
Joachim Bauch 2014-04-18 00:13:15 +02:00
parent b03dcbad2a
commit 8827bd1a23
3 changed files with 21 additions and 22 deletions

View File

@ -26,15 +26,17 @@ import (
"unicode/utf8" "unicode/utf8"
) )
// echoCopy echoes messages from the client using io.Copy. var upgrader = websocket.Upgrader{
func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
u := websocket.Upgrader{
ReadBufferSize: 4096, ReadBufferSize: 4096,
WriteBufferSize: 4096, WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool { CheckOrigin: func(r *http.Request) bool {
return true return true
}} },
conn, err := u.Upgrade(w, r, nil) }
// echoCopy echoes messages from the client using io.Copy.
func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
conn, err := upgrader.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)
@ -93,13 +95,7 @@ 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) {
u := websocket.Upgrader{ conn, err := upgrader.Upgrade(w, r, nil)
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
return true
}}
conn, err := u.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Println("Upgrade:", err) log.Println("Upgrade:", err)
return return

View File

@ -25,6 +25,11 @@ const (
maxMessageSize = 512 maxMessageSize = 512
) )
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// connection is an middleman between the websocket connection and the hub. // connection is an middleman between the websocket connection and the hub.
type connection struct { type connection struct {
// The websocket connection. // The websocket connection.
@ -89,10 +94,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", 405) http.Error(w, "Method not allowed", 405)
return return
} }
u := websocket.Upgrader{ ws, err := upgrader.Upgrade(w, r, nil)
ReadBufferSize: 1024,
WriteBufferSize: 1024}
ws, err := u.Upgrade(w, r, nil)
if err != nil { if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok { if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err) log.Println(err)

View File

@ -35,6 +35,10 @@ var (
addr = flag.String("addr", ":8080", "http service address") addr = flag.String("addr", ":8080", "http service address")
homeTempl = template.Must(template.New("").Parse(homeHTML)) homeTempl = template.Must(template.New("").Parse(homeHTML))
filename string filename string
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
) )
func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) { func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
@ -107,10 +111,7 @@ func writer(ws *websocket.Conn, lastMod time.Time) {
} }
func serveWs(w http.ResponseWriter, r *http.Request) { func serveWs(w http.ResponseWriter, r *http.Request) {
u := websocket.Upgrader{ ws, err := upgrader.Upgrade(w, r, nil)
ReadBufferSize: 1024,
WriteBufferSize: 1024}
ws, err := u.Upgrade(w, r, nil)
if err != nil { if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok { if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err) log.Println(err)