fix args error for DownloadFile
fix bug for watch
This commit is contained in:
parent
93cfbde981
commit
5352152b66
14
go.mod
14
go.mod
@ -3,10 +3,10 @@ module apigo.cc/gojs/service
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.7
|
||||
apigo.cc/gojs v0.0.8
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
apigo.cc/gojs/http v0.0.3
|
||||
apigo.cc/gojs/util v0.0.7
|
||||
apigo.cc/gojs/util v0.0.8
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/ssgo/config v1.7.9
|
||||
github.com/ssgo/discover v1.7.9
|
||||
@ -21,7 +21,7 @@ require (
|
||||
require (
|
||||
github.com/ZZMarquis/gm v1.3.2 // indirect
|
||||
github.com/dlclark/regexp2 v1.11.4 // indirect
|
||||
github.com/emmansun/gmsm v0.29.4 // indirect
|
||||
github.com/emmansun/gmsm v0.29.6 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||
@ -36,9 +36,9 @@ require (
|
||||
github.com/tklauser/go-sysconf v0.3.14 // indirect
|
||||
github.com/tklauser/numcpus v0.9.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
golang.org/x/crypto v0.29.0 // indirect
|
||||
golang.org/x/net v0.31.0 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/text v0.20.0 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/net v0.32.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
@ -111,7 +111,7 @@ func (r *Response) SendFile(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Val
|
||||
|
||||
func (r *Response) DownloadFile(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
||||
args := gojs.MakeArgs(&argsIn, vm).Check(3)
|
||||
r.resp.DownloadFile(args.Str(0), args.Str(1), args.Bytes(1))
|
||||
r.resp.DownloadFile(args.Str(0), args.Str(1), args.Bytes(2))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
44
service.go
44
service.go
@ -214,20 +214,13 @@ func init() {
|
||||
// 处理Watch
|
||||
if vm.GoData["inWatch"] == true {
|
||||
onWatchConn := map[string]*websocket.Conn{}
|
||||
onWatchLock := sync.RWMutex{}
|
||||
onWatchLock := sync.Mutex{}
|
||||
vm.GoData["onWatch"] = func(filename string) {
|
||||
conns := map[string]*websocket.Conn{}
|
||||
onWatchLock.RLock()
|
||||
onWatchLock.Lock()
|
||||
defer onWatchLock.Unlock()
|
||||
for id, conn := range onWatchConn {
|
||||
conns[id] = conn
|
||||
}
|
||||
onWatchLock.RUnlock()
|
||||
for id, conn := range conns {
|
||||
if err := conn.WriteMessage(websocket.TextMessage, []byte(filename)); err != nil {
|
||||
onWatchLock.Lock()
|
||||
delete(onWatchConn, id)
|
||||
onWatchLock.Unlock()
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -246,11 +239,38 @@ func init() {
|
||||
outStr := u.String(out)
|
||||
// 注入自动刷新的代码
|
||||
outStr = strings.ReplaceAll(outStr, "</html>", `<script>
|
||||
let _watchWS = null
|
||||
let _watchWSConnection = false
|
||||
let _watchWSIsFirst = true
|
||||
function connect() {
|
||||
_watchWSConnection = true
|
||||
let ws = new WebSocket(location.protocol.replace('http', 'ws') + '//' + location.host + '/_watch')
|
||||
ws.onmessage = () => { location.reload() }
|
||||
ws.onclose = () => { setTimeout(connect, 1000) }
|
||||
ws.onopen = () => {
|
||||
_watchWS = ws
|
||||
_watchWSConnection = false
|
||||
if( !_watchWSIsFirst ) location.reload()
|
||||
_watchWSIsFirst = false
|
||||
}
|
||||
ws.onmessage = () => {
|
||||
location.reload()
|
||||
}
|
||||
ws.onclose = () => {
|
||||
_watchWS = null
|
||||
_watchWSConnection = false
|
||||
}
|
||||
}
|
||||
setInterval(()=>{
|
||||
if(_watchWS!= null){
|
||||
try{
|
||||
_watchWS.send("ping")
|
||||
}catch(err){
|
||||
_watchWS = null
|
||||
_watchWSConnection = false
|
||||
}
|
||||
} else if(!_watchWSConnection){
|
||||
connect()
|
||||
}
|
||||
}, 1000)
|
||||
connect()
|
||||
</script>
|
||||
</html>`)
|
||||
|
@ -271,6 +271,7 @@ interface Response {
|
||||
sendFile: (contentType: string, filename: string) => void
|
||||
downloadFile: (contentType: string, filename: string, data: any) => void
|
||||
location: (url: string) => void
|
||||
end: (data: any) => void
|
||||
}
|
||||
|
||||
interface Logger {
|
||||
|
Loading…
Reference in New Issue
Block a user