add http caller example

This commit is contained in:
vedhavyas committed 2019-09-23 22:22:23 +02:00
1 parent f6ac4e734a
commit ff4ee892fe
4 files changed
+99 -1

No files matched your search

+35
View File
@@ -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)
}
+29
View File
@@ -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 {}
}
BIN
View File
Binary file not shown.