mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-10-05 09:53: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:
86
handlers/ws.go
Normal file
86
handlers/ws.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/franela/play-with-docker/services"
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
session := services.GetSession(sessionId)
|
||||
instance := services.GetInstance(session, instanceName)
|
||||
|
||||
if instance.Stdout == nil {
|
||||
id, err := services.CreateExecConnection(instance.Name, ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn, err := services.AttachExecConnection(id, ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
encoder := encoding.Replacement.NewEncoder()
|
||||
instance.Conn = conn
|
||||
instance.Stdout = &cookoo.MultiWriter{}
|
||||
instance.Stdout.Init()
|
||||
u1 := uuid.NewV4()
|
||||
instance.Stdout.AddWriter(u1.String(), ws)
|
||||
go func() {
|
||||
io.Copy(encoder.Writer(instance.Stdout), instance.Conn.Reader)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
go func() {
|
||||
io.Copy(instance.Conn.Conn, ws)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
} else {
|
||||
u1 := uuid.NewV4()
|
||||
instance.Stdout.AddWriter(u1.String(), ws)
|
||||
|
||||
go func() {
|
||||
io.Copy(instance.Conn.Conn, ws)
|
||||
instance.Stdout.RemoveWriter(u1.String())
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user