Switch to gorilla/websocket

This commit is contained in:
khlieng 2015-05-02 00:20:22 +02:00
parent c7bf27f8aa
commit d1a82a65b5
41 changed files with 3269 additions and 2304 deletions

View file

@ -7,8 +7,8 @@ import (
"strings"
"sync"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/gorilla/websocket"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/julienschmidt/httprouter"
"github.com/khlieng/name_pending/Godeps/_workspace/src/golang.org/x/net/websocket"
"github.com/khlieng/name_pending/storage"
)
@ -19,6 +19,11 @@ var (
sessionLock sync.Mutex
fs http.Handler
files []File
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
type File struct {
@ -49,13 +54,53 @@ func Run(port int, development bool) {
router := httprouter.New()
router.Handler("GET", "/ws", websocket.Handler(handleWS))
router.HandlerFunc("GET", "/ws", upgradeWS)
router.NotFound = serveFiles
log.Println("Listening on port", port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), router))
}
func upgradeWS(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
handleWS(conn)
}
func serveFiles(w http.ResponseWriter, r *http.Request) {
var ext string
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
ext = ".gz"
}
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html")
r.URL.Path = "/index.html" + ext
fs.ServeHTTP(w, r)
return
}
for _, file := range files {
if strings.HasSuffix(r.URL.Path, file.Path) {
w.Header().Set("Content-Type", file.ContentType)
r.URL.Path = file.Path + ext
fs.ServeHTTP(w, r)
return
}
}
w.Header().Set("Content-Type", "text/html")
r.URL.Path = "/index.html" + ext
fs.ServeHTTP(w, r)
}
func reconnect() {
for _, user := range storage.LoadUsers() {
session := NewSession()
@ -92,33 +137,3 @@ func reconnect() {
}
}
}
func serveFiles(w http.ResponseWriter, r *http.Request) {
var ext string
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
ext = ".gz"
}
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html")
r.URL.Path = "/index.html" + ext
fs.ServeHTTP(w, r)
return
}
for _, file := range files {
if strings.HasSuffix(r.URL.Path, file.Path) {
w.Header().Set("Content-Type", file.ContentType)
r.URL.Path = file.Path + ext
fs.ServeHTTP(w, r)
return
}
}
w.Header().Set("Content-Type", "text/html")
r.URL.Path = "/index.html" + ext
fs.ServeHTTP(w, r)
}

View file

@ -4,7 +4,8 @@ import (
"encoding/json"
"sync"
"github.com/khlieng/name_pending/Godeps/_workspace/src/golang.org/x/net/websocket"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/gorilla/websocket"
"github.com/khlieng/name_pending/storage"
)

View file

@ -1,7 +1,7 @@
package server
import (
"github.com/khlieng/name_pending/Godeps/_workspace/src/golang.org/x/net/websocket"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/gorilla/websocket"
)
type WebSocket struct {
@ -19,6 +19,6 @@ func NewWebSocket(ws *websocket.Conn) *WebSocket {
func (w *WebSocket) write() {
for {
w.conn.Write(<-w.Out)
w.conn.WriteMessage(websocket.TextMessage, <-w.Out)
}
}

View file

@ -5,7 +5,8 @@ import (
"log"
"strings"
"github.com/khlieng/name_pending/Godeps/_workspace/src/golang.org/x/net/websocket"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/gorilla/websocket"
"github.com/khlieng/name_pending/storage"
)
@ -16,12 +17,12 @@ func handleWS(ws *websocket.Conn) {
var UUID string
var req WSRequest
addr := ws.Request().RemoteAddr
addr := ws.RemoteAddr().String()
log.Println(addr, "connected")
for {
err := websocket.JSON.Receive(ws, &req)
err := ws.ReadJSON(&req)
if err != nil {
if session != nil {
session.deleteWS(addr)