func getUrl(url string, urc chan urlReply) urlReply { httpHandler := hx.New("", "haxe.Http", 1, url) hx.Code("", "_a.param(0).val.onData=_a.param(1).val;", httpHandler, hx.CallbackFunc( func(data string) { urc <- urlReply{err: nil, dat: data} })) hx.Code("", "_a.param(0).val.onError=_a.param(1).val;", httpHandler, hx.CallbackFunc( func(msg string) { urc <- urlReply{err: errors.New(msg), dat: ""} })) hx.Meth("", httpHandler, "haxe.Http", "request", 0) return <-urc }
// Main allows code that might be targeted to broser-based JS code to schedule TARDISgo code periodically, separate from call-backs. // For non-JS targets it simply calls the given mainFN. // This should be the last function called in main.main() func BrowserMain(mainFN func(), msInvocationInterval, runLimit int) { if runtime.GOARCH == "js" { // probably only want to do this in a browser JScallbackOK = true ht := hx.New("js", "haxe.Timer", 1, msInvocationInterval) go func() { // put the main program into a goroutine mainFN() hx.Meth("js", ht, "haxe.Timer", "stop", 0) }() hx.SetInt("js", "Scheduler.runLimit", runLimit) hx.Code("js", "_a.param(0).val.run=Scheduler.timerEventHandler;", ht) } else { mainFN() } }
func (c Conn) Go(serviceMethod string, args, reply interface{}, done chan *Call) *Call { call := &Call{ ServiceMethod: serviceMethod, Args: args, Reply: reply, Error: nil, Done: done, } c.mutex.Lock() hx.Meth("", c.conn, "TgoConnect", "setErrorHandler", 1, func(e uintptr) { call.Error = errors.New(hx.CallString("", "Std.string", 1, e)) done <- call c.mutex.Unlock() }) //fmt.Printf("DEBUG args=%v:%T\n", args, args) var networkOut bytes.Buffer enc := gob.NewEncoder(&networkOut) err := enc.Encode(args) if err != nil { call.Error = err done <- call c.mutex.Unlock() return call } var networkOut2 bytes.Buffer enc2 := gob.NewEncoder(&networkOut2) err = enc2.Encode(reply) if err != nil { call.Error = err done <- call c.mutex.Unlock() return call } ifa := []interface{}{serviceMethod, base64.StdEncoding.EncodeToString(networkOut.Bytes()), base64.StdEncoding.EncodeToString(networkOut2.Bytes())} //fmt.Printf("DEBUG ifa %#v:%T\n", ifa, ifa) haxeArgs := interfaceToDynamic(ifa) //fmt.Println("DEBUG haxe ifa", hx.CallString("", "Std.string", 1, haxeArgs)) hx.Meth("", c.conn, "TgoConnect", "call", 2, haxeArgs, func(r uintptr) { c.mutex.Unlock() // another call can begin before processing what's below //hx.Call("", "trace", 1, r) rA, ok := dynamicToInterface(r).([]interface{}) if !ok { call.Error = errors.New("returned value not an []interface{} in tgocall") } else { errMsg, ok := rA[1].(string) if !ok { call.Error = errors.New("returned error message not a string in tgocall") } else { if errMsg != "" { call.Error = errors.New(errMsg) } else { back64, ok := rA[0].(string) //println("back64: " + back64) if !ok { call.Error = errors.New("returned encoded data not a string in tgocall") } else { backBuf, err64 := base64.StdEncoding.DecodeString(back64) if err64 != nil { //println("DEBUG TgoConnect returned base64 string " + // back64 + " has error " + err64.Error()) call.Error = err64 } else { networkBack := bytes.NewBuffer(backBuf) dec := gob.NewDecoder(networkBack) err = dec.Decode(call.Reply) if err != nil { call.Error = err } } } } } } done <- call }) return call }