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 {
|
for k, v := range mc.Msg {
|
||||||
msg[k] = v
|
msg[k] = v
|
||||||
}
|
}
|
||||||
// obj := adapters[i].vm.NewObject()
|
obj := adapters[i].vm.NewObject()
|
||||||
// for k, v := range mc.Msg {
|
for k, v := range mc.Msg {
|
||||||
// obj.Set(k, v)
|
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.Set("bot_id", adapters[i].botid)
|
||||||
resolve(mc.Msg)
|
// msg["bot_id"] = adapters[i].botid
|
||||||
// adapters[i].vm.NewProxy(obj, &goja.ProxyTrapConfig{
|
// msg["setMessageId"] = func(id string) {
|
||||||
// Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
|
|
||||||
// if property == "message_id" {
|
|
||||||
// select {
|
// select {
|
||||||
// case <-mc.Chan:
|
// case <-mc.Chan:
|
||||||
// return false
|
|
||||||
// case <-time.After(time.Millisecond):
|
// 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)
|
}(i)
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-2
@@ -23,15 +23,42 @@ type HttpListen struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var httpListens sync.Map
|
var httpListens sync.Map
|
||||||
|
var httpListensAny sync.Map
|
||||||
var listenCounter2 int64
|
var listenCounter2 int64
|
||||||
|
|
||||||
func AddHttpListen(api, method string, vm *goja.Runtime, uuid string, resolve func(result interface{}), reject func(reason interface{})) {
|
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{
|
hl := &HttpListen{
|
||||||
Path: api,
|
Path: api,
|
||||||
Method: method,
|
Method: method,
|
||||||
Chan: make(chan *RR, 1),
|
UUID: uuid,
|
||||||
}
|
}
|
||||||
|
key := atomic.AddInt64(&listenCounter2, 1)
|
||||||
|
hl.Chan = make(chan *RR, 1)
|
||||||
httpListens.Store(key, hl)
|
httpListens.Store(key, hl)
|
||||||
f, ok := <-hl.Chan
|
f, ok := <-hl.Chan
|
||||||
if ok {
|
if ok {
|
||||||
@@ -53,4 +80,15 @@ func CancelHttpListen(uuid string) {
|
|||||||
}
|
}
|
||||||
return true
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/cdle/sillyplus/utils"
|
"github.com/cdle/sillyplus/utils"
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
@@ -62,14 +61,9 @@ func (r *Response) Json(ps ...interface{}) *Response {
|
|||||||
}
|
}
|
||||||
d, err := json.Marshal(data)
|
d, err := json.Marshal(data)
|
||||||
f := string(d)
|
f := string(d)
|
||||||
|
r.status = int(status)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if strings.Contains(f, `"error"`) {
|
r.content = f
|
||||||
f = strings.Replace(f, "error", "msg", 1)
|
|
||||||
if status == 200 {
|
|
||||||
status = -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r.content = strings.Replace(f, "{", fmt.Sprintf(`{"status":%d, `, status), 1)
|
|
||||||
r.isJson = true
|
r.isJson = true
|
||||||
} else {
|
} else {
|
||||||
r.content += fmt.Sprint(data)
|
r.content += fmt.Sprint(data)
|
||||||
|
|||||||
+41
-1
@@ -87,6 +87,47 @@ func init() {
|
|||||||
var res = &Response{
|
var res = &Response{
|
||||||
c: c,
|
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 {
|
httpListens.Range(func(key, value any) bool {
|
||||||
web := value.(*HttpListen)
|
web := value.(*HttpListen)
|
||||||
if web.Closed {
|
if web.Closed {
|
||||||
@@ -108,7 +149,6 @@ func init() {
|
|||||||
httpListens.Delete(key)
|
httpListens.Delete(key)
|
||||||
req.handled = true
|
req.handled = true
|
||||||
var end = make(chan bool, 1)
|
var end = make(chan bool, 1)
|
||||||
// fmt.Println(c.Request.URL.Path, key)
|
|
||||||
web.Chan <- &RR{
|
web.Chan <- &RR{
|
||||||
Req: req,
|
Req: req,
|
||||||
Res: res,
|
Res: res,
|
||||||
|
|||||||
Reference in New Issue
Block a user