This commit is contained in:
hujiabin 2023-10-25 15:56:24 +08:00
parent 67ec492463
commit dd01c08cca

View File

@ -8,7 +8,7 @@ import (
http的请求分为两种风格 http的请求分为两种风格
go style: go风格回调的时候不会join主线程被回调方需要处理线程同步问题 go style: go风格回调的时候不会join主线程被回调方需要处理线程同步问题
js style: js风格回调时候会自动join主线程被回调方无需处理线程同步问题 js style: js风格回调时候会自动join主线程被回调方无需处理线程同步问题
*/ */
const ( const (
GO_STYLE_REQUEST = 1 GO_STYLE_REQUEST = 1
@ -33,14 +33,13 @@ type httpCliResponse struct {
} }
type httpCliMgr struct { type httpCliMgr struct {
} }
func (this *httpCliMgr) init() { func (this *httpCliMgr) init() {
_app.RegisterIMMsgHandle( _app.RegisterIMMsgHandle(
IM_HTTP_CLI_MGR_RESPONSE, IM_HTTP_CLI_MGR_RESPONSE,
func (args q5.Args) { func(args q5.Args) {
cb := args[0].(func (HttpCliResponse)) cb := args[0].(func(HttpCliResponse))
rsp := args[1].(*httpCliResponse) rsp := args[1].(*httpCliResponse)
cb(rsp) cb(rsp)
}) })
@ -51,7 +50,7 @@ func (this *httpCliMgr) unInit() {
func (this *httpCliMgr) SendGoStyleRequest( func (this *httpCliMgr) SendGoStyleRequest(
url string, params map[string]string, url string, params map[string]string,
cb func (HttpCliResponse)) { cb func(HttpCliResponse)) {
this.internalSendRequest( this.internalSendRequest(
GO_STYLE_REQUEST, GO_STYLE_REQUEST,
NORMAL_CHANNEL, NORMAL_CHANNEL,
@ -60,9 +59,31 @@ func (this *httpCliMgr) SendGoStyleRequest(
cb) cb)
} }
func (this *httpCliMgr) SyncSendGoStyleRequest(
url string, params map[string]string,
cb func(HttpCliResponse)) {
chDone := make(chan bool)
this.internalSendRequest(
GO_STYLE_REQUEST,
NORMAL_CHANNEL,
url,
params,
func(rsp HttpCliResponse) {
cb(rsp)
chDone <- true
})
for {
select {
case <-chDone:
close(chDone)
return
}
}
}
func (this *httpCliMgr) SendJsStyleRequest( func (this *httpCliMgr) SendJsStyleRequest(
url string, params map[string]string, url string, params map[string]string,
cb func (HttpCliResponse)) { cb func(HttpCliResponse)) {
this.internalSendRequest( this.internalSendRequest(
JS_STYLE_REQUEST, JS_STYLE_REQUEST,
NORMAL_CHANNEL, NORMAL_CHANNEL,
@ -73,7 +94,7 @@ func (this *httpCliMgr) SendJsStyleRequest(
func (this *httpCliMgr) SendQuickChannelGoStyleRequest( func (this *httpCliMgr) SendQuickChannelGoStyleRequest(
url string, params map[string]string, url string, params map[string]string,
cb func (HttpCliResponse)) { cb func(HttpCliResponse)) {
this.internalSendRequest( this.internalSendRequest(
GO_STYLE_REQUEST, GO_STYLE_REQUEST,
QUICK_CHANNEL, QUICK_CHANNEL,
@ -84,7 +105,7 @@ func (this *httpCliMgr) SendQuickChannelGoStyleRequest(
func (this *httpCliMgr) SendQuickChannelJsStyleRequest( func (this *httpCliMgr) SendQuickChannelJsStyleRequest(
url string, params map[string]string, url string, params map[string]string,
cb func (HttpCliResponse)) { cb func(HttpCliResponse)) {
this.internalSendRequest( this.internalSendRequest(
JS_STYLE_REQUEST, JS_STYLE_REQUEST,
QUICK_CHANNEL, QUICK_CHANNEL,
@ -96,7 +117,7 @@ func (this *httpCliMgr) SendQuickChannelJsStyleRequest(
func (this *httpCliMgr) internalSendRequest( func (this *httpCliMgr) internalSendRequest(
style int32, channel int32, style int32, channel int32,
url string, params map[string]string, url string, params map[string]string,
cb func (HttpCliResponse)) { cb func(HttpCliResponse)) {
if !(style == GO_STYLE_REQUEST || style == JS_STYLE_REQUEST) { if !(style == GO_STYLE_REQUEST || style == JS_STYLE_REQUEST) {
panic("httpCliMgr sytel error") panic("httpCliMgr sytel error")
} }
@ -113,7 +134,7 @@ func (this *httpCliMgr) internalSendRequest(
if style == GO_STYLE_REQUEST { if style == GO_STYLE_REQUEST {
cb(rsp) cb(rsp)
} else { } else {
_app.AddIMMsg(IM_HTTP_CLI_MGR_RESPONSE, []interface{} { _app.AddIMMsg(IM_HTTP_CLI_MGR_RESPONSE, []interface{}{
cb, cb,
rsp, rsp,
}) })