script.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build js
  2. package main
  3. import (
  4. "fmt"
  5. "net"
  6. "net/rpc/jsonrpc"
  7. "time"
  8. "github.com/gopherjs/websocket"
  9. "honnef.co/go/js/dom"
  10. )
  11. var document = dom.GetWindow().Document()
  12. var output = document.GetElementByID("output").(*dom.HTMLPreElement)
  13. func appendOutput(s string) {
  14. output.SetTextContent(output.TextContent() + s)
  15. }
  16. type Args struct {
  17. A, B int
  18. }
  19. var conn net.Conn
  20. func main() {
  21. var err error
  22. conn, err = websocket.Dial("ws://localhost:8880/rpc-websocket")
  23. if err != nil {
  24. panic(err)
  25. }
  26. client := jsonrpc.NewClient(conn)
  27. for i := 0; i < 100; i++ {
  28. started := time.Now()
  29. args := &Args{15, 3}
  30. var reply int
  31. {
  32. err := client.Call("Arith.Multiply", args, &reply)
  33. if err != nil {
  34. fmt.Println("arith error:", err)
  35. }
  36. }
  37. appendOutput(fmt.Sprintf("Arith: %d*%d=%d taken %v\n", args.A, args.B, reply, time.Since(started).String()))
  38. time.Sleep(5 * time.Second)
  39. }
  40. err = client.Close()
  41. if err != nil {
  42. fmt.Println("client.Close():", err)
  43. }
  44. err = conn.Close()
  45. if err != nil {
  46. fmt.Println("conn.Close():", err)
  47. }
  48. }