jjzjj

http.Client 在编译为 wasm 的 Go 中不起作用

coder 2023-06-30 原文

我想使用 http.Client 进行具有相同 X-Header 的简单 HTTP GET,但是在 println("Start: getValues (client.Do)").

错误在代码编译为 wasm 时发生。

我需要同样的帮助来理解这个错误。

代码

println("Start: getValues")
client := &http.Client{

}

req, err := http.NewRequest("GET", "https://abc.azurewebsites.net/api/Value", nil)
if err != nil {
    log.Println(err.Error())
    return nil
}

req.Header.Add("X-ApiKey", `ABC`)
req.Header.Add("X-System", `ABC`)
req.Header.Add("X-User", `ABC`)

println("Start: getValues (client.Do)")
// EXCEPTION !!!
resp, err := client.Do(req)
if err != nil {
    return nil
}

println("Start: getValues (ReadAll)")
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return nil
}

浏览器记录

wasm_exec.js:45 Hello World!
wasm_exec.js:45 WASM Go Initialized
wasm_exec.js:45 
wasm_exec.js:45 Start: GetData
wasm_exec.js:45 Start: getValues
wasm_exec.js:45 Start: getValues (client.Do)
wasm_exec.js:45 fatal error: all goroutines are asleep - deadlock!
wasm_exec.js:45 
wasm_exec.js:45 goroutine 1 [chan receive]:
wasm_exec.js:45 main.main()
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:43 +0xa
wasm_exec.js:45 
wasm_exec.js:45 goroutine 5 [select]:
wasm_exec.js:45 net/http.(*Transport).RoundTrip(0x36b100, 0xc0ba000, 0x0, 0x0, 0x0)
wasm_exec.js:45     C:/Go/src/net/http/roundtrip_js.go:151 +0x44
wasm_exec.js:45 net/http.send(0xc0ba000, 0xbdc80, 0x36b100, 0x0, 0x0, 0x0, 0xc00c068, 0x22, 0xc028de0, 0x1)
wasm_exec.js:45     C:/Go/src/net/http/client.go:250 +0x16
wasm_exec.js:45 net/http.(*Client).send(0xc028d80, 0xc0ba000, 0x0, 0x0, 0x0, 0xc00c068, 0x0, 0x1, 0x40000000000)
wasm_exec.js:45     C:/Go/src/net/http/client.go:174 +0x10
wasm_exec.js:45 net/http.(*Client).do(0xc028d80, 0xc0ba000, 0x0, 0x0, 0x0)
wasm_exec.js:45     C:/Go/src/net/http/client.go:641 +0x17
wasm_exec.js:45 net/http.(*Client).Do(0xc028d80, 0xc0ba000, 0x6, 0x89f48, 0x24)
wasm_exec.js:45     C:/Go/src/net/http/client.go:509 +0x2
wasm_exec.js:45 main.getValues(0x84392, 0x12, 0x0, 0x10b0003d, 0xc048150)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:124 +0x10
wasm_exec.js:45 main.GetData(0x7ff800000000001f, 0x83468)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:53 +0x5
wasm_exec.js:45 main.getValue(0xc040240, 0x3, 0x3)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:161 +0x1d
wasm_exec.js:45 syscall/js.callbackLoop()
wasm_exec.js:45     C:/Go/src/syscall/js/callback.go:116 +0x7
wasm_exec.js:45 created by syscall/js.NewCallback.func1
wasm_exec.js:45     C:/Go/src/syscall/js/callback.go:40 +0x2
wasm_exec.js:67 exit code: 2
exit @ wasm_exec.js:67
runtime.wasmExit @ wasm_exec.js:185
wasm-function[939]
wasm-function[928]
wasm-function[857]
wasm-function[883]
wasm-function[472]
wasm-function[470]
wasm-function[591]
wasm-function[594]
wasm-function[546]
wasm-function[555]
wasm-function[559]
wasm-function[561]
wasm-function[882]
wasm-function[499]
wasm-function[637]
wasm-function[4291]
wasm-function[926]
run @ wasm_exec.js:383
wasm_exec.js:378 Uncaught (in promise) Error: bad callback: Go program has already exited
    at global.Go._resolveCallbackPromise (wasm_exec.js:378)
    at wasm_exec.js:394
_resolveCallbackPromise @ wasm_exec.js:378
(anonymous) @ wasm_exec.js:394

最佳答案

所以似乎 http 调用(在 wasm 中)阻塞了我的主 js 应用程序中的事件线程,因此出现了死锁。在 go 例程中运行 http 请求为我修复了它。

https://github.com/golang/go/issues/26382#issuecomment-416926829

关于http.Client 在编译为 wasm 的 Go 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54042683/

有关http.Client 在编译为 wasm 的 Go 中不起作用的更多相关文章

  1. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  2. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  3. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  4. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  5. c - mkmf 在编译 C 扩展时忽略子文件夹中的文件 - 2

    我想这样组织C源代码:+/||___+ext||||___+native_extension||||___+lib||||||___(Sourcefilesarekeptinhere-maycontainsub-folders)||||___native_extension.c||___native_extension.h||___extconf.rb||___+lib||||___(Rubysourcecode)||___Rakefile我无法使此设置与mkmf一起正常工作。native_extension/lib中的文件(包含在native_extension.c中)将被完全忽略。

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - Rails - 从命名路由中提取 HTTP 动词 - 2

    Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba

  8. ruby-on-rails - Heroku 吃掉了我的自定义 HTTP header - 2

    我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份​​验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby​​脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara

  9. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

  10. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

随机推荐