add http caller example
This commit is contained in:
4 files changed
+99
-1
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/vedhavyas/go-wasm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := wasm.BridgeFromFile("test", "./examples/http/main.wasm", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx, canc := context.WithCancel(context.Background())
|
||||
defer canc()
|
||||
init := make(chan error)
|
||||
go b.Run(ctx, init)
|
||||
if err := <-init; err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res, err := b.CallFunc("call", []interface{}{"https://google.com"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
str, err := wasm.String(res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("Result:", str)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func call(this js.Value, args []js.Value) interface{} {
|
||||
res, err := http.Get(args[0].String())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
f := fmt.Sprintln(res.Status, res.StatusCode, res.ContentLength)
|
||||
err = res.Body.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func main() {
|
||||
js.Global().Set("call", js.FuncOf(call))
|
||||
select {}
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user