/
page.go
226 lines (203 loc) · 4.81 KB
/
page.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
package easyweb
import (
"encoding/json"
"fmt"
"strings"
"github.com/gorilla/websocket"
"github.com/lengzhao/easyweb/util"
)
var _ Page = &easyPage{}
func newPage(conn *websocket.Conn) *easyPage {
var page easyPage
page.conn = conn
page.callback = make(map[string]eventMsgData)
page.msgChan = make(chan any, 10)
page.closed = make(chan int)
page.env = make(map[string]any)
page.watchEnv = make(map[string]func(value any))
return &page
}
func (p *easyPage) Title(title string) Page {
id := util.GetCallerID(util.LevelParent)
msg := toClientMsgData{id, "title", title}
p.sendMsg(msg)
return p
}
func (p *easyPage) AddJs(js string) Page {
id := util.GetCallerID(util.LevelParent)
msg := toClientMsgData{id, "js", js}
p.sendMsg(msg)
return p
}
func (p *easyPage) RunJs(js string) Page {
id := util.GetCallerID(util.LevelParent)
msg := toClientMsgData{id, "eval", js}
p.sendMsg(msg)
return p
}
func (p *easyPage) GetPeer() string {
return p.conn.RemoteAddr().String()
}
func (p *easyPage) AddCss(css string) Page {
id := util.GetCallerID(util.LevelParent)
msg := toClientMsgData{id, "css", css}
p.sendMsg(msg)
return p
}
// e 将作为子元素放到div内,如<div id=rand_id>e</div>
func (p *easyPage) Write(e any) string {
var id string
gid, ok := e.(IContainerID)
if ok {
id = gid.ContainerID()
}
if id == "" {
id = util.GetCallerID(util.LevelParent)
}
return p.WriteWithID(id, e)
}
// e 将作为子元素放到id所属的元素内,如<div id=id>e</div>
func (p *easyPage) WriteWithID(id string, e any) string {
msg := toClientMsgData{id, "", fmt.Sprint(e)}
p.sendMsg(msg)
if evt, ok := e.(IAfterLoaded); ok {
evt.AfterElementLoadedFromFramwork(p)
}
return id
}
func (p *easyPage) Delete(id string) {
msg := toClientMsgData{id, "", ""}
p.sendMsg(msg)
}
type attrInfo struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
// 修改指定id的元素的属性
func (p *easyPage) SetAttr(id, key, value string) string {
info := attrInfo{Key: key, Value: value}
data, _ := json.Marshal(info)
msg := toClientMsgData{id, "attr", string(data)}
p.sendMsg(msg)
return id
}
// 注册指定id的前端事件,typ是前端事件名,cb是回调还是,如果cb为空,则是取消事件回调
func (p *easyPage) RegistEvent(id, typ string, cb IMessageCb) {
if id == "" || typ == "" {
return
}
// 1. add event callback(server side)
cbMsg := eventMsgData{}
cbMsg.ID = id
cbMsg.Event = cb
p.sendMsg(cbMsg)
// 2. add client event(jquery will handle the event)
if cb != nil {
toClient := toClientMsgData{id, "event", typ}
p.sendMsg(toClient)
} else {
toClient := toClientMsgData{id, "off", typ}
p.sendMsg(toClient)
}
}
func (p *easyPage) sendMsg(msg any) {
select {
case <-p.closed:
case p.msgChan <- msg:
}
}
func (p *easyPage) Close() {
select {
case <-p.closed:
default:
p.conn.Close()
}
}
func (p *easyPage) WaitUntilClosed() {
<-p.closed
}
func (p *easyPage) SetEnv(key string, value any) {
p.mu.Lock()
p.env[key] = value
cb := p.watchEnv[key]
p.mu.Unlock()
if cb != nil {
cb(value)
}
}
func (p *easyPage) GetEnv(key string) (value any) {
p.mu.Lock()
value = p.env[key]
p.mu.Unlock()
return
}
func (p *easyPage) WatchEnv(key string, cb func(value any)) error {
p.mu.Lock()
defer p.mu.Unlock()
if p.watchEnv[key] != nil {
return fmt.Errorf("exist callback")
}
p.watchEnv[key] = cb
return nil
}
func encode(v interface{}) []byte {
buff := new(strings.Builder)
enc := json.NewEncoder(buff)
enc.SetEscapeHTML(false)
enc.Encode(v)
return []byte(buff.String())
}
func (p *easyPage) processMsg() {
for {
select {
case <-p.closed:
return
case data, ok := <-p.msgChan:
if !ok {
continue
}
switch msg := data.(type) {
case toClientMsgData:
if msg.ID == "" {
continue
}
p.conn.WriteMessage(websocket.TextMessage, encode(msg))
if msg.Msg == "" {
delete(p.callback, msg.ID)
}
case fromClientMsgData:
cb := p.callback[msg.ID]
if cb.Event != nil {
go func(id string, dataType CbDataType, data []byte) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered", r)
}
}()
cb.Event.MessageCallbackFromFramwork(msg.ID, dataType, data)
}(msg.ID, CbDataTypeString, []byte(msg.Msg))
}
case fileMsgData:
cb := p.callback[msg.ID]
if cb.Event != nil {
go func(id string, dataType CbDataType, data []byte) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered", r)
}
}()
cb.Event.MessageCallbackFromFramwork(msg.ID, dataType, data)
}(msg.ID, CbDataTypeBinary, msg.BinaryData)
}
case eventMsgData:
if msg.Event == nil {
delete(p.callback, msg.ID)
} else {
p.callback[msg.ID] = msg
}
default:
fmt.Println("unknown msg type:", msg)
}
}
}
}