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