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"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// echoCopy echoes messages from the client using io.Copy.
func echoCopy(w http.ResponseWriter, r *http.Request, writerOnly bool) {
u := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
return true
}}
conn, err := u.Upgrade(w, r, nil)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
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
// with ioutil.ReadAll.
func echoReadAll(w http.ResponseWriter, r *http.Request, writeMessage bool) {
u := websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
CheckOrigin: func(r *http.Request) bool {
return true
}}
conn, err := u.Upgrade(w, r, nil)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return

View File

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

View File

@ -35,6 +35,10 @@ var (
addr = flag.String("addr", ":8080", "http service address")
homeTempl = template.Must(template.New("").Parse(homeHTML))
filename string
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
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) {
u := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024}
ws, err := u.Upgrade(w, r, nil)
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)