1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 17:33:21 +08:00

Huge refactor to have everything working with socket.io

It fixes lots of bugs, can fallback to long polling, resize viewport of
terminals and share clients state of the session, so they all see the
same thing.
This commit is contained in:
Jonathan Leibiusky (@xetorthio)
2016-11-10 10:42:08 -03:00
parent 9b6991f130
commit 8e4981d24f
16 changed files with 566 additions and 249 deletions

View File

@@ -4,12 +4,13 @@ import (
"net/http"
"github.com/franela/play-with-docker/services"
"github.com/go-zoo/bone"
"github.com/gorilla/mux"
)
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
instanceName := bone.GetValue(req, "instanceName")
vars := mux.Vars(req)
sessionId := vars["sessionId"]
instanceName := vars["instanceName"]
s := services.GetSession(sessionId)
i := services.GetInstance(s, instanceName)

View File

@@ -2,14 +2,17 @@ package handlers
import (
"encoding/json"
"log"
"net/http"
"github.com/franela/play-with-docker/services"
"github.com/go-zoo/bone"
"github.com/gorilla/mux"
)
func GetSession(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
vars := mux.Vars(req)
sessionId := vars["sessionId"]
log.Println(sessionId)
session := services.GetSession(sessionId)

View File

@@ -6,16 +6,18 @@ import (
"net/http"
"github.com/franela/play-with-docker/services"
"github.com/go-zoo/bone"
"github.com/gorilla/mux"
)
func NewInstance(rw http.ResponseWriter, req *http.Request) {
sessionId := bone.GetValue(req, "sessionId")
vars := mux.Vars(req)
sessionId := vars["sessionId"]
s := services.GetSession(sessionId)
s.Lock()
if len(s.Instances) >= 5 {
s.Unlock()
rw.WriteHeader(http.StatusConflict)
return
}

View File

@@ -1,22 +1,42 @@
package handlers
import (
"io"
"log"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
"golang.org/x/text/encoding"
"github.com/franela/play-with-docker/cookoo"
"github.com/franela/play-with-docker/services"
"github.com/go-zoo/bone"
"github.com/twinj/uuid"
"github.com/googollee/go-socket.io"
"github.com/gorilla/mux"
)
// Echo the data received on the WebSocket.
func Exec(ws *websocket.Conn) {
sessionId := bone.GetValue(ws.Request(), "sessionId")
instanceName := bone.GetValue(ws.Request(), "instanceName")
func WS(so socketio.Socket) {
vars := mux.Vars(so.Request())
sessionId := vars["sessionId"]
session := services.GetSession(sessionId)
if session == nil {
log.Printf("Session with id [%s] does not exist!\n", sessionId)
return
}
session.AddNewClient(services.NewClient(so, session))
}
func WSError(so socketio.Socket) {
log.Println("error ws")
}
/*
so.Join(sessionId)
// TODO: Reset terminal geometry
so.On("resize", func(cols, rows int) {
// TODO: Reset terminal geometry
})
so.On("disconnection", func() {
//TODO: reset the best terminal geometry
})
ctx := context.Background()
@@ -62,5 +82,5 @@ func Exec(ws *websocket.Conn) {
case <-ctx.Done():
}
}
}
*/