service.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //@Author : KaiShin
  2. //@Time : 2022/3/16
  3. package main
  4. import (
  5. "bufio"
  6. "call_center/call/rpc/call"
  7. "call_center/call/rpc/pb"
  8. "call_center/demo/data"
  9. "context"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/tal-tech/go-zero/core/logx"
  13. "github.com/tal-tech/go-zero/zrpc"
  14. "io"
  15. "log"
  16. "os"
  17. "strings"
  18. )
  19. func serviceLogin() {
  20. conf := new(zrpc.RpcClientConf)
  21. conf.Target = data.Addr
  22. data.Client = call.NewCall(zrpc.MustNewClient(*conf))
  23. req := new(call.ServiceMsgReq)
  24. cmd := pb.CommandMsg{}
  25. idInfo := pb.IdInfo{GameId: data.GameId, Id: data.ServiceId}
  26. req.Cmd = append(req.Cmd, &cmd)
  27. req.IdInfo = &idInfo
  28. stream, err := data.Client.ServiceLogin(context.Background(), req)
  29. if err != nil {
  30. logx.Errorf("playerLogin failed, playerId:%s, err:%s", err)
  31. return
  32. }
  33. for {
  34. res, err := stream.Recv()
  35. if err == io.EOF {
  36. logx.Info("收到服务端的结束信号")
  37. break
  38. }
  39. if err != nil {
  40. logx.Info("接收数据错误:", err)
  41. }
  42. if res == nil {
  43. break
  44. }
  45. for _, cmd := range res.Cmd {
  46. if cmd.CmdType != pb.ECommand_MSG_HEART_BEAT {
  47. logx.Infof("[DEBUG] 收到cmd:%v, val:%v, str:%v, buff:%s", cmd.CmdType, cmd.CmdVal, cmd.CmdStr, cmd.GetBuff())
  48. }
  49. switch cmd.CmdType {
  50. case pb.ECommand_ON_PLAYER_RECEIVE_REPLY:
  51. data.PlayerId = cmd.CmdStr
  52. logx.Info("分配到玩家:", data.PlayerId)
  53. case pb.ECommand_SEND_MSG:
  54. buff := cmd.GetChatMsg()
  55. logx.Info("[客戶端收到]:", buff.Input)
  56. case pb.ECommand_ON_PLAYER_DISCONNECT:
  57. if data.ServiceId == cmd.CmdStr {
  58. data.PlayerId = "default"
  59. logx.Infof("玩家[%v]已断开\n", cmd.CmdStr)
  60. }
  61. case pb.ECommand_ON_SERVICE_CONNECT:
  62. data.ServiceId = cmd.CmdStr
  63. case pb.ECommand_ON_PLAYER_CONNECT:
  64. // 玩家登录
  65. if data.PlayerId == "default" {
  66. data.PlayerId = cmd.CmdStr
  67. logx.Infof("玩家{%v}登录,客服{%v}当前空闲,已分配", data.PlayerId, data.ServiceId)
  68. }
  69. default:
  70. break
  71. }
  72. }
  73. }
  74. }
  75. func serviceOnCall() {
  76. input := bufio.NewReader(os.Stdin)
  77. for {
  78. logx.Info("请输入信息:\n")
  79. chatContent, _ := input.ReadString('\n')
  80. if data.Client == nil {
  81. logx.Error("service not login, client is null")
  82. continue
  83. }
  84. var proto = new(pb.ServiceMsgReq)
  85. var cmd = new(pb.CommandMsg)
  86. cmd = parseGmCmd(chatContent)
  87. if cmd != nil {
  88. } else {
  89. cmd = new(pb.CommandMsg)
  90. cmd.CmdType = pb.ECommand_CALL_SERVICE_MSG
  91. var chatMsg = new(pb.CommandMsg_ChatMsg)
  92. chatMsg.ChatMsg = &pb.ChatMsg{ClientId: data.PlayerId, Input: chatContent}
  93. cmd.Buff = chatMsg
  94. }
  95. proto.Cmd = append(proto.Cmd, cmd)
  96. proto.IdInfo = &pb.IdInfo{Id: data.ServiceId}
  97. if res, err := data.Client.ServiceCall(context.Background(), proto); err != nil {
  98. fmt.Println("error:", err)
  99. continue
  100. } else {
  101. if res != nil {
  102. log.Println(res)
  103. }
  104. }
  105. }
  106. }
  107. func parseGmCmd(cmd string) *pb.CommandMsg {
  108. /*
  109. 客服连接
  110. {"cmd_type": 2005, "cmd_val": 1, "cmd_str": "1648121824824636975904"}
  111. 获取聊天日志
  112. {"cmd_type": 2006, "cmd_val": 2, "cmd_str": "16474122581374391221744"}
  113. */
  114. ok := strings.Contains(cmd, "cmd_type")
  115. if ok != true {
  116. return nil
  117. }
  118. cmd = strings.Replace(cmd, "\n", "", -1)
  119. cmdMsg := new(pb.CommandMsg)
  120. var cmdStr map[string]interface{}
  121. err := json.Unmarshal([]byte(cmd), &cmdStr)
  122. if err != nil {
  123. log.Println("<ParseCmd> err:", err)
  124. return nil
  125. }
  126. cmdTpe := int32(cmdStr["cmd_type"].(float64))
  127. cmdVal := int32(cmdStr["cmd_val"].(float64))
  128. cmdMsg.CmdType = pb.ECommand(cmdTpe)
  129. cmdMsg.CmdVal = cmdVal
  130. cmdMsg.CmdStr = cmdStr["cmd_str"].(string)
  131. return cmdMsg
  132. }
  133. func main() {
  134. go serviceLogin()
  135. serviceOnCall()
  136. }