x
This commit is contained in:
+21
-21
@@ -269,33 +269,33 @@ func GetReplyMessage(vm *goja.Runtime, plt string, bots_id []string) *goja.Promi
|
||||
for k, v := range mc.Msg {
|
||||
msg[k] = v
|
||||
}
|
||||
// obj := adapters[i].vm.NewObject()
|
||||
// for k, v := range mc.Msg {
|
||||
// obj.Set(k, v)
|
||||
// }
|
||||
// obj.Set("bot_id", adapters[i].botid)
|
||||
msg["bot_id"] = adapters[i].botid
|
||||
msg["setMessageId"] = func(id string) {
|
||||
select {
|
||||
case <-mc.Chan:
|
||||
case <-time.After(time.Millisecond):
|
||||
mc.Chan <- id
|
||||
obj := adapters[i].vm.NewObject()
|
||||
for k, v := range mc.Msg {
|
||||
obj.Set(k, v)
|
||||
}
|
||||
}
|
||||
resolve(mc.Msg)
|
||||
// adapters[i].vm.NewProxy(obj, &goja.ProxyTrapConfig{
|
||||
// Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
|
||||
// if property == "message_id" {
|
||||
obj.Set("bot_id", adapters[i].botid)
|
||||
// msg["bot_id"] = adapters[i].botid
|
||||
// msg["setMessageId"] = func(id string) {
|
||||
// select {
|
||||
// case <-mc.Chan:
|
||||
// return false
|
||||
// case <-time.After(time.Millisecond):
|
||||
// mc.Chan <- id
|
||||
// }
|
||||
// mc.Chan <- fmt.Sprint(value.Export())
|
||||
// }
|
||||
// return true
|
||||
// },
|
||||
// })
|
||||
// resolve(msg)
|
||||
resolve(vm.NewProxy(obj, &goja.ProxyTrapConfig{
|
||||
Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
|
||||
if property == "message_id" {
|
||||
select {
|
||||
case <-mc.Chan:
|
||||
return false
|
||||
case <-time.After(time.Millisecond):
|
||||
}
|
||||
mc.Chan <- fmt.Sprint(value.Export())
|
||||
}
|
||||
return true
|
||||
},
|
||||
}))
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
+40
-2
@@ -23,15 +23,42 @@ type HttpListen struct {
|
||||
}
|
||||
|
||||
var httpListens sync.Map
|
||||
var httpListensAny sync.Map
|
||||
var listenCounter2 int64
|
||||
|
||||
func AddHttpListen(api, method string, vm *goja.Runtime, uuid string, resolve func(result interface{}), reject func(reason interface{})) {
|
||||
key := atomic.AddInt64(&listenCounter2, 1)
|
||||
if method == "ANY" {
|
||||
var hl *HttpListen
|
||||
v, ok := httpListensAny.Load(uuid)
|
||||
if ok {
|
||||
// logs.Debug("load", uuid)
|
||||
hl = v.(*HttpListen)
|
||||
} else {
|
||||
hl = &HttpListen{
|
||||
Path: api,
|
||||
Method: method,
|
||||
Chan: make(chan *RR, 1000),
|
||||
}
|
||||
// logs.Debug("crate", uuid)
|
||||
httpListensAny.Store(uuid, hl)
|
||||
}
|
||||
f, ok := <-hl.Chan
|
||||
if ok {
|
||||
// logs.Debug("http resolved")
|
||||
resolve(f)
|
||||
} else {
|
||||
// logs.Debug("script is stopped")
|
||||
reject(Error(vm, "script is stopped"))
|
||||
}
|
||||
return
|
||||
}
|
||||
hl := &HttpListen{
|
||||
Path: api,
|
||||
Method: method,
|
||||
Chan: make(chan *RR, 1),
|
||||
UUID: uuid,
|
||||
}
|
||||
key := atomic.AddInt64(&listenCounter2, 1)
|
||||
hl.Chan = make(chan *RR, 1)
|
||||
httpListens.Store(key, hl)
|
||||
f, ok := <-hl.Chan
|
||||
if ok {
|
||||
@@ -53,4 +80,15 @@ func CancelHttpListen(uuid string) {
|
||||
}
|
||||
return true
|
||||
})
|
||||
httpListensAny.Range(func(key, value any) bool {
|
||||
hl := value.(*HttpListen)
|
||||
if hl.UUID == uuid {
|
||||
httpListens.Delete(key)
|
||||
if !hl.Closed {
|
||||
hl.Closed = true
|
||||
close(hl.Chan)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package core
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/cdle/sillyplus/utils"
|
||||
"github.com/dop251/goja"
|
||||
@@ -62,14 +61,9 @@ func (r *Response) Json(ps ...interface{}) *Response {
|
||||
}
|
||||
d, err := json.Marshal(data)
|
||||
f := string(d)
|
||||
r.status = int(status)
|
||||
if err == nil {
|
||||
if strings.Contains(f, `"error"`) {
|
||||
f = strings.Replace(f, "error", "msg", 1)
|
||||
if status == 200 {
|
||||
status = -1
|
||||
}
|
||||
}
|
||||
r.content = strings.Replace(f, "{", fmt.Sprintf(`{"status":%d, `, status), 1)
|
||||
r.content = f
|
||||
r.isJson = true
|
||||
} else {
|
||||
r.content += fmt.Sprint(data)
|
||||
|
||||
+41
-1
@@ -87,6 +87,47 @@ func init() {
|
||||
var res = &Response{
|
||||
c: c,
|
||||
}
|
||||
|
||||
httpListensAny.Range(func(_, value any) bool {
|
||||
web := value.(*HttpListen)
|
||||
if web.Closed {
|
||||
return true
|
||||
}
|
||||
req.handled = true
|
||||
var end = make(chan bool, 1)
|
||||
web.Chan <- &RR{
|
||||
Req: req,
|
||||
Res: res,
|
||||
End: func() {
|
||||
if !web.Closed {
|
||||
end <- true
|
||||
}
|
||||
},
|
||||
}
|
||||
select {
|
||||
case <-end:
|
||||
// fmt.Println("end", req.handled)
|
||||
case <-time.After(time.Second * 10):
|
||||
logs.Warn("%s 响应超时", c.Request.URL.Path)
|
||||
}
|
||||
close(end)
|
||||
if req.handled {
|
||||
if res.isRedirect {
|
||||
return false
|
||||
}
|
||||
if res.isJson {
|
||||
c.Header("Content-Type", "application/json")
|
||||
}
|
||||
// fmt.Println(res.status, res.content)
|
||||
c.String(res.status, res.content)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if req.handled {
|
||||
return
|
||||
}
|
||||
httpListens.Range(func(key, value any) bool {
|
||||
web := value.(*HttpListen)
|
||||
if web.Closed {
|
||||
@@ -108,7 +149,6 @@ func init() {
|
||||
httpListens.Delete(key)
|
||||
req.handled = true
|
||||
var end = make(chan bool, 1)
|
||||
// fmt.Println(c.Request.URL.Path, key)
|
||||
web.Chan <- &RR{
|
||||
Req: req,
|
||||
Res: res,
|
||||
|
||||
Reference in New Issue
Block a user