-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
227 lines (197 loc) · 5.53 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
/*
cd cmd/wasm/
#cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ../../assets/
GOOS=js GOARCH=wasm go build -o ../../assets/json.wasm
cd -
*/
import (
"encoding/json"
"fmt"
"syscall/js"
)
var (
document js.Value = js.Global().Get("document")
window js.Value = js.Global().Get("window")
console js.Value = js.Global().Get("console")
webSocket js.Value = js.Global().Get("WebSocket")
loc js.Value = window.Get("location")
uri string = "ws:"
ws js.Value
protocol string = loc.Get("protocol").String()
)
type MsgEvent struct {
Type string `json:"type"`
ID string `json:"id"`
Event string `json:"event"`
Data string `json:"data"`
}
type DataEventIn struct {
ID string `json:"id"`
IdRet string `json:"id_ret"`
Type string `json:"type"`
Value interface{} `json:"value"`
Propertie string `json:"propertie"`
SubType string `json:"sub_type"`
}
type DataEventOut struct {
Type string `json:"type"`
IdRet string `json:"id_ret"`
Data interface{} `json:"data"`
}
func connect() {
document = js.Global().Get("document")
window = js.Global().Get("window")
console = js.Global().Get("console")
webSocket = js.Global().Get("WebSocket")
loc = window.Get("location")
uri = "ws:"
protocol = loc.Get("protocol").String()
fmt.Println("Go Web LiveView")
if protocol == "https:" {
uri = "wss:"
}
fmt.Println("protocol: " + protocol + " uri: " + uri)
uri += "//" + loc.Get("host").String()
uri += loc.Get("pathname").String() + "ws_goliveview"
ws = webSocket.New(uri)
handlerOnOpen := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
fmt.Println(ws.Get("readyState").Int())
fmt.Println("Connected...ok!!")
return nil
})
handlerOnClose := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
fmt.Println(ws)
fmt.Println("Disconnected...ok")
document.Call("getElementById", "content").Set("innerHTML", "Disconnected")
return nil
})
handlerOnMessage := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
evtData := args[0].Get("data").String()
var dataEventIn DataEventIn
json.Unmarshal([]byte(evtData), &dataEventIn)
currentElement := document.Call("getElementById", dataEventIn.ID)
if currentElement.IsNull() {
return nil
}
if dataEventIn.Type == "fill" {
fmt.Println("fill")
currentElement.Set("innerHTML", dataEventIn.Value)
return nil
}
if dataEventIn.Type == "remove" {
currentElement.Call("remove")
}
if dataEventIn.Type == "addNode" {
var d = document.Call("createElement", "div")
currentElement.Set("innerHTML", fmt.Sprint(dataEventIn.Value))
currentElement.Call("appendChild", d)
}
if dataEventIn.Type == "text" {
if dataEventIn.Value != "" {
currentElement.Set("innerText", dataEventIn.Value)
}
}
if dataEventIn.Type == "style" {
currentElement.Get("style").Set("cssText", dataEventIn.Value)
}
if dataEventIn.Type == "set" {
currentElement.Set("value", dataEventIn.Value)
}
if dataEventIn.Type == "script" {
currentElement.Call("eval", dataEventIn.Value)
}
if dataEventIn.Type == "propertie" {
currentElement.Set(dataEventIn.Propertie, dataEventIn.Value)
}
if dataEventIn.Type == "get" {
dataEventOut := DataEventOut{}
dataEventOut.Type = "get"
dataEventOut.IdRet = dataEventIn.IdRet
if dataEventIn.SubType == "value" {
value := currentElement.Get("value")
dataEventOut.Data = GetValue(value)
}
if dataEventIn.SubType == "html" {
value := currentElement.Get("innerHTML")
dataEventOut.Data = GetValue(value)
}
if dataEventIn.SubType == "text" {
value := currentElement.Get("innerText")
dataEventOut.Data = GetValue(value)
}
if dataEventIn.SubType == "style" {
value := currentElement.Get("style").Get(fmt.Sprint(dataEventIn.Value))
dataEventOut.Data = GetValue(value)
}
if dataEventIn.SubType == "propertie" {
prop := currentElement.Get(fmt.Sprint(dataEventIn.Value))
dataEventOut.Data = GetValue(prop)
}
jsonBytes, _ := json.Marshal(&dataEventOut)
ws.Call("send", string(jsonBytes))
}
return nil
})
fmt.Println("Set handlers...??")
ws.Set("onclose", handlerOnClose)
ws.Set("onopen", handlerOnOpen)
ws.Set("onmessage", handlerOnMessage)
fmt.Println("Set handlers...ok!!")
}
func main() {
document.Call("getElementById", "content").Set("innerHTML", "Disconnected")
connect()
js.Global().Call("setInterval", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if ws.Get("readyState").Int() != 1 {
connect()
}
return nil
}), 1000)
js.Global().Set("ws", ws)
js.Global().Set("connect", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
connect()
return nil
}))
js.Global().Set("send_event", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
id := args[0].String()
event := args[1].String()
data := ""
if len(args) == 3 {
data = args[2].String()
}
sendEvent(id, event, data)
return nil
}))
<-make(chan struct{})
}
func GetValue(prop js.Value) interface{} {
switch prop.Type() {
case js.TypeBoolean:
return prop.Bool()
case js.TypeNumber:
return prop.Int()
case js.TypeNull:
return nil
case js.TypeString:
return prop.String()
case js.TypeUndefined:
return nil
}
return nil
}
func sendEvent(id string, event string, data string) {
msgEvent := MsgEvent{
Type: "data",
ID: id,
Event: event,
Data: data,
}
jsonMsg, _ := json.Marshal(&msgEvent)
ws.Call("send", string(jsonMsg))
}