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

View File

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