dropped fiber for std http + gorilla websocket
Session serialization will use gob encoding instead of json. Binary size will likely be reduced. General backend code refactoring.
This commit is contained in:
53
server/rpc/handlers.go
Normal file
53
server/rpc/handlers.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var upgrader websocket.Upgrader
|
||||
|
||||
func WebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer c.Close()
|
||||
|
||||
// notify client that conn is open and ok
|
||||
c.WriteJSON(struct{ Status string }{Status: "connected"})
|
||||
|
||||
for {
|
||||
mtype, reader, err := c.NextReader()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
break
|
||||
}
|
||||
|
||||
res := newRequest(reader).Call()
|
||||
|
||||
writer, err := c.NextWriter(mtype)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
break
|
||||
}
|
||||
|
||||
io.Copy(writer, res)
|
||||
}
|
||||
}
|
||||
|
||||
func Post(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
res := newRequest(r.Body).Call()
|
||||
_, err := io.Copy(w, res)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user