golang-im聊天
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

185 Zeilen
4.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "gim/pkg/pb"
  5. "gim/pkg/util"
  6. "log"
  7. "net"
  8. "time"
  9. gim_util "github.com/alberliu/gn/util"
  10. jsoniter "github.com/json-iterator/go"
  11. "google.golang.org/protobuf/proto"
  12. )
  13. var codecFactory = gim_util.NewHeaderLenCodecFactory(2, 1024)
  14. func main() {
  15. log.SetFlags(log.Lshortfile | log.LstdFlags)
  16. client := TcpClient{}
  17. log.Println("input UserId,DeviceId,SyncSeq")
  18. fmt.Scanf("%d %d %d", &client.UserId, &client.DeviceId, &client.Seq)
  19. client.Start()
  20. select {}
  21. }
  22. func Json(i interface{}) string {
  23. bytes, _ := jsoniter.Marshal(i)
  24. return string(bytes)
  25. }
  26. type TcpClient struct {
  27. UserId int64
  28. DeviceId int64
  29. Seq int64
  30. codec *gim_util.Codec
  31. Conn net.Conn
  32. }
  33. func (c *TcpClient) Output(pt pb.PackageType, requestId int64, message proto.Message) {
  34. var input = pb.Input{
  35. Type: pt,
  36. RequestId: requestId,
  37. }
  38. if message != nil {
  39. bytes, err := proto.Marshal(message)
  40. if err != nil {
  41. log.Println(err)
  42. return
  43. }
  44. input.Data = bytes
  45. }
  46. inputByf, err := proto.Marshal(&input)
  47. if err != nil {
  48. log.Println(err)
  49. return
  50. }
  51. _, err = c.Conn.Write(gim_util.Encode(inputByf))
  52. if err != nil {
  53. log.Println(err)
  54. }
  55. }
  56. func (c *TcpClient) Start() {
  57. //connect, err := net.Dial("tcp", "111.229.238.28:8080")
  58. connect, err := net.Dial("tcp", "im-tcp-connect.izhyin.com:8001")
  59. if err != nil {
  60. log.Println(err)
  61. return
  62. }
  63. c.codec = codecFactory.NewCodec(connect)
  64. c.Conn = connect
  65. c.SignIn()
  66. c.SyncTrigger()
  67. c.SubscribeRoom()
  68. go c.Heartbeat()
  69. go c.Receive()
  70. }
  71. func (c *TcpClient) SignIn() {
  72. signIn := pb.SignInInput{
  73. UserId: c.UserId,
  74. DeviceId: c.DeviceId,
  75. Token: "RRTTGFUKCAQGAIDXMITFIBPMZOVSYDQQUQRZNMYG",
  76. }
  77. c.Output(pb.PackageType_PT_SIGN_IN, time.Now().UnixNano(), &signIn)
  78. }
  79. func (c *TcpClient) SyncTrigger() {
  80. c.Output(pb.PackageType_PT_SYNC, time.Now().UnixNano(), &pb.SyncInput{Seq: c.Seq})
  81. }
  82. func (c *TcpClient) Heartbeat() {
  83. ticker := time.NewTicker(time.Minute * 5)
  84. for range ticker.C {
  85. c.Output(pb.PackageType_PT_HEARTBEAT, time.Now().UnixNano(), nil)
  86. }
  87. }
  88. func (c *TcpClient) Receive() {
  89. for {
  90. bytes, err := c.codec.Read()
  91. if err != nil {
  92. log.Println(err)
  93. return
  94. }
  95. c.HandlePackage(bytes)
  96. }
  97. }
  98. func (c *TcpClient) SubscribeRoom() {
  99. c.Output(pb.PackageType_PT_SUBSCRIBE_ROOM, 0, &pb.SubscribeRoomInput{
  100. RoomId: 1,
  101. Seq: 0,
  102. })
  103. }
  104. func (c *TcpClient) HandlePackage(bytes []byte) {
  105. var output pb.Output
  106. err := proto.Unmarshal(bytes, &output)
  107. if err != nil {
  108. log.Println(err)
  109. return
  110. }
  111. switch output.Type {
  112. case pb.PackageType_PT_SIGN_IN:
  113. log.Println(Json(&output))
  114. case pb.PackageType_PT_HEARTBEAT:
  115. log.Println("心跳响应")
  116. case pb.PackageType_PT_SYNC:
  117. log.Println("离线消息同步开始------")
  118. syncResp := pb.SyncOutput{}
  119. err := proto.Unmarshal(output.Data, &syncResp)
  120. if err != nil {
  121. log.Println(err)
  122. return
  123. }
  124. log.Println("离线消息同步响应:code", output.Code, "message:", output.Message)
  125. for _, msg := range syncResp.Messages {
  126. log.Printf("消息:发送者类型:%d 发送者id:%d 接收者类型:%d 接收者id:%d 消息内容:%+v seq:%d \n",
  127. msg.Sender.SenderType, msg.Sender.SenderId, msg.ReceiverType, msg.ReceiverId, util.FormatMessage(msg.MessageType, msg.MessageContent), msg.Seq)
  128. c.Seq = msg.Seq
  129. }
  130. ack := pb.MessageACK{
  131. DeviceAck: c.Seq,
  132. ReceiveTime: util.UnixMilliTime(time.Now()),
  133. }
  134. c.Output(pb.PackageType_PT_MESSAGE, output.RequestId, &ack)
  135. log.Println("离线消息同步结束------")
  136. case pb.PackageType_PT_MESSAGE:
  137. messageSend := pb.MessageSend{}
  138. err := proto.Unmarshal(output.Data, &messageSend)
  139. if err != nil {
  140. log.Println(err)
  141. return
  142. }
  143. msg := messageSend.Message
  144. //if msg.Sender.SenderType == 1 {
  145. // //系统发送的消息
  146. //
  147. //}
  148. log.Printf("消息:发送者类型:%d 发送者id:%d 接收者类型:%d 接收者id:%d 消息内容:%+v seq:%d \n",
  149. msg.Sender.SenderType, msg.Sender.SenderId, msg.ReceiverType, msg.ReceiverId, util.FormatMessage(msg.MessageType, msg.MessageContent), msg.Seq)
  150. c.Seq = msg.Seq
  151. ack := pb.MessageACK{
  152. DeviceAck: msg.Seq,
  153. ReceiveTime: util.UnixMilliTime(time.Now()),
  154. }
  155. c.Output(pb.PackageType_PT_MESSAGE, output.RequestId, &ack)
  156. default:
  157. log.Println("switch other")
  158. }
  159. }