chat_update.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package model
  2. import (
  3. "rocommon"
  4. "rocommon/util"
  5. "roserver/baseserver"
  6. "roserver/serverproto"
  7. "runtime/debug"
  8. )
  9. type ChatUpdateItemList struct {
  10. ChatList []ChatUpdateItem
  11. }
  12. type ChatUpdateItem struct {
  13. UserId string
  14. RoleUid uint64
  15. RoleName string
  16. ZoneId int32
  17. Content []string
  18. }
  19. type ChatUpdateMag struct {
  20. chatContentList map[uint64]*ChatUpdateItem
  21. updateTimer util.ServerTimer //更新定时器
  22. }
  23. func newChatUpdateMag() *ChatUpdateMag {
  24. mag := &ChatUpdateMag{
  25. chatContentList: map[uint64]*ChatUpdateItem{},
  26. }
  27. mag.updateTimer = util.NewDurationTimer(util.GetCurrentTime(), 1000)
  28. return mag
  29. }
  30. func (this *ChatUpdateMag) Update(ms uint64) {
  31. if this.updateTimer.IsStart() && this.updateTimer.IsExpired(ms) {
  32. if len(this.chatContentList) <= 0 {
  33. return
  34. }
  35. //send to gmweb
  36. urlPath := "/chat"
  37. var tmpChatList ChatUpdateItemList
  38. for key, val := range this.chatContentList {
  39. tmpChatList.ChatList = append(tmpChatList.ChatList, *val)
  40. delete(this.chatContentList, key)
  41. }
  42. go func() {
  43. defer func() {
  44. //打印奔溃信息
  45. if err := recover(); err != nil {
  46. util.InfoF("onError data=%v \n%s\n", err, string(debug.Stack()))
  47. }
  48. }()
  49. tmpRequest := &rocommon.HTTPRequest{}
  50. tmpRequest.ReqCodecName = "httpjson"
  51. tmpRequest.ReqMsg = tmpChatList
  52. parm := GetHttpNodeParam()
  53. httpNode := baseserver.CreateHttpConnector(parm)
  54. err := httpNode.(rocommon.HTTPConnector).Request("POST", urlPath, tmpRequest)
  55. if err != nil {
  56. util.InfoF("http Request err=%v", err)
  57. return
  58. }
  59. }()
  60. }
  61. }
  62. func (this *ChatUpdateMag) AddChatMsg(msg *serverproto.SSWebGMChatMsgNtf) {
  63. addItem, ok := this.chatContentList[msg.Uid]
  64. if !ok {
  65. addItem = &ChatUpdateItem{
  66. RoleUid: msg.Uid,
  67. RoleName: msg.ChatSelfName,
  68. ZoneId: msg.SelectZoneId,
  69. Content: []string{msg.Content.Message},
  70. }
  71. this.chatContentList[msg.Uid] = addItem
  72. } else {
  73. addItem.Content = append(addItem.Content, msg.Content.Message)
  74. }
  75. }