client.go 911 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package client
  2. import (
  3. "bet24.com/log"
  4. "context"
  5. "encoding/json"
  6. )
  7. type Request struct {
  8. Msg string
  9. Data string
  10. }
  11. type Reply struct {
  12. Data string
  13. RetCode int
  14. }
  15. type Request_Notification struct {
  16. UserId int
  17. Data string
  18. }
  19. func DoRequest(addr, msg, data string) (bool, *Reply) {
  20. xclient := getClientMgr().getClient(addr)
  21. if xclient == nil {
  22. log.Release("msg=%s data=%s call failed: client==nil", msg, data)
  23. return false, nil
  24. }
  25. args := &Request{
  26. Msg: msg,
  27. Data: data,
  28. }
  29. reply := &Reply{}
  30. err := xclient.Call(context.Background(), msg, args, reply)
  31. if err != nil {
  32. log.Release("msg=%s data=%s call failed: %v", msg, data, err)
  33. return false, nil
  34. }
  35. return true, reply
  36. }
  37. func OnNotification(addr string, userId int, data string) {
  38. msg := "OnNotification"
  39. d, _ := json.Marshal(Request_Notification{
  40. UserId: userId,
  41. Data: data,
  42. })
  43. DoRequest(addr, msg, string(d))
  44. }