-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremoteApp.go
138 lines (115 loc) · 3.3 KB
/
remoteApp.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package tsremote
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"github.com/jkoenig134/ts-remote-app/event"
"github.com/jkoenig134/ts-remote-app/publisher"
"github.com/gorilla/websocket"
)
type RemoteApp struct {
apiKeyProvider ApiKeyProvider
c *websocket.Conn
authorized bool
eventPublisher publisher.AppClientEventPublisher
wsUrl string
}
type NewTsAppParams struct {
Host string
Port int
}
func NewTsApp(provider ApiKeyProvider, p NewTsAppParams) RemoteApp {
if p.Host == "" {
p.Host = "localhost"
}
if p.Port == 0 {
p.Port = 5899
}
wsUrl := fmt.Sprintf("ws://%s:%d", p.Host, p.Port)
return RemoteApp{
apiKeyProvider: provider,
authorized: false,
eventPublisher: publisher.NewParser(),
wsUrl: wsUrl,
}
}
func (app *RemoteApp) Disconnect() {
if app.c != nil {
_ = app.c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
_ = app.c.Close()
app.c = nil
}
app.authorized = false
app.eventPublisher.PublishEvent(event.DisconnectEvent)
}
func (app *RemoteApp) Connect() error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
c, _, err := websocket.DefaultDialer.Dial(app.wsUrl, nil)
if err != nil {
return err
}
app.c = c
go app.receive()
return nil
}
func (app *RemoteApp) SubscribeEvent(event event.AppEvent, fn interface{}) error {
return app.eventPublisher.SubscribeEvent(event, fn)
}
func (app *RemoteApp) UnsubscribeEvent(event event.AppEvent, handler interface{}) error {
return app.eventPublisher.UnsubscribeEvent(event, handler)
}
type TypeScan struct {
Type string `json:"type"`
}
func (app *RemoteApp) receive() {
defer app.Disconnect()
for {
_, message, err := app.c.ReadMessage()
if err != nil {
return
}
resp := TypeScan{}
if err = json.Unmarshal(message, &resp); err != nil {
app.eventPublisher.PublishEvent(event.ErrorEvent, fmt.Errorf("deserialize: %s", message))
continue
}
switch resp.Type {
case "auth":
if key := app.eventPublisher.HandleAuthResponse(message); key != nil {
app.authorized = true
app.apiKeyProvider.SetApiKey(*key)
}
case "clientSelfPropertyUpdated":
app.eventPublisher.HandleClientSelfPropertyUpdated(message)
case "clientPropertiesUpdated":
app.eventPublisher.HandleClientPropertyUpdated(message)
case "connectStatusChanged":
app.eventPublisher.HandleConnectStatusChanged(message)
case "channelsSubscribed":
app.eventPublisher.HandleChannelsSubscribed(message)
case "clientMoved":
app.eventPublisher.HandleClientMoved(message)
case "channelPropertiesUpdated":
app.eventPublisher.HandleChannelPropertiesUpdated(message)
case "clientChannelGroupChanged":
app.eventPublisher.HandleClientChannelGroupChanged(message)
case "channels":
app.eventPublisher.HandleChannels(message)
case "groupInfo":
app.eventPublisher.HandleGroupInfo(message)
case "permissionList":
app.eventPublisher.HandlePermissionList(message)
case "serverPropertiesUpdated":
app.eventPublisher.HandleServerPropertiesUpdated(message)
case "neededPermissions":
app.eventPublisher.HandleNeededPermissions(message)
case "buttonPress":
app.eventPublisher.HandleButtonPress(message)
default:
log.Printf("not recognized event %s: %s\n", resp.Type, string(message))
}
}
}