basic.go 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. //go:generate folder2go -handler web webGenerated
  3. import (
  4. "log"
  5. "net/http"
  6. "time"
  7. "dev.hexasoftware.com/stdio/wsrpc"
  8. "dev.hexasoftware.com/stdio/wsrpc/sample/basic/webGenerated"
  9. "github.com/gohxs/webu"
  10. )
  11. func MyCliFunc(cli *wsrpc.ClientCtx) {
  12. cli.Define("btn1.click", func(param ...interface{}) interface{} {
  13. log.Println("Async button clicked")
  14. return "The thing clicked in the button"
  15. })
  16. for { // do multiple calls
  17. select {
  18. case <-time.After(10 * time.Second):
  19. ret := cli.Call("Hello", wsrpc.DataObj{
  20. "couldbe": "interface",
  21. })
  22. log.Println("Response:", ret)
  23. }
  24. }
  25. }
  26. func main() {
  27. var mux = http.NewServeMux()
  28. var rpc = wsrpc.New(MyCliFunc)
  29. mux.Handle("/wsrpc/", webu.LogHandler("wsrpc", rpc.ServeHTTP))
  30. mux.HandleFunc("/", webu.LogHandler("/", webGenerated.AssetHandleFunc))
  31. log.Println("Listening :8080")
  32. err := http.ListenAndServe(":8081", mux)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }