main.go 864 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/cryptix/exp/unixsockmux/client"
  5. "github.com/cryptix/go/logging"
  6. "io"
  7. "log"
  8. "os"
  9. "time"
  10. )
  11. var check = logging.CheckFatal
  12. const path = "/tmp/unixsockmux.sock"
  13. func main() {
  14. c, err := client.NewClient(path)
  15. check(err)
  16. rwc, err := c.OpenChannel("ping", struct{ Foo int }{23})
  17. check(err)
  18. go func() {
  19. for i:=5;i>0;i--{
  20. fmt.Fprintln(rwc, "ping",i)
  21. time.Sleep(250 * time.Millisecond)
  22. }
  23. rwc.Close()
  24. }()
  25. go func() {
  26. io.Copy(os.Stderr, rwc)
  27. log.Println("ping input closed")
  28. }()
  29. echo, err := c.OpenChannel("echo", 42)
  30. check(err)
  31. go func() {
  32. io.Copy(os.Stdout, echo)
  33. }()
  34. for i,s := range []string{"hello", "world", "sup?"} {
  35. fmt.Fprintln(echo,s,i)
  36. log.Println("echoed",s)
  37. time.Sleep(1*time.Second)
  38. }
  39. //io.Copy(echo, os.Stdin)
  40. log.Println("echo closed")
  41. echo.Close()
  42. c.Close()
  43. }