forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.go
92 lines (77 loc) · 1.82 KB
/
ws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package handlers
import (
"fmt"
"log"
"github.com/franela/play-with-docker/services"
"github.com/googollee/go-socket.io"
"github.com/gorilla/mux"
)
func WS(so socketio.Socket) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from ", r)
}
}()
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():
}
}
}
*/