conn.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package http
  2. import (
  3. "io/ioutil"
  4. pb "go-common/app/service/main/broadcast/api/grpc/v1"
  5. "go-common/library/ecode"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/gogo/protobuf/proto"
  8. )
  9. func connect(ctx *bm.Context) {
  10. query := ctx.Request.URL.Query()
  11. if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
  12. ctx.Protobuf(nil, ecode.Unauthorized)
  13. return
  14. }
  15. b, err := ioutil.ReadAll(ctx.Request.Body)
  16. if err != nil {
  17. ctx.Protobuf(nil, err)
  18. return
  19. }
  20. var req pb.ConnectReq
  21. if err = proto.Unmarshal(b, &req); err != nil {
  22. ctx.Protobuf(nil, err)
  23. return
  24. }
  25. mid, key, room, platform, accepts, err := srv.Connect(ctx, req.Server, req.ServerKey, req.Cookie, req.Token)
  26. if err != nil {
  27. ctx.Protobuf(nil, err)
  28. return
  29. }
  30. ctx.Protobuf(&pb.ConnectReply{Mid: mid, Key: key, RoomID: room, Accepts: accepts, Platform: platform}, nil)
  31. }
  32. func disconnect(ctx *bm.Context) {
  33. query := ctx.Request.URL.Query()
  34. if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
  35. ctx.Protobuf(nil, ecode.Unauthorized)
  36. return
  37. }
  38. b, err := ioutil.ReadAll(ctx.Request.Body)
  39. if err != nil {
  40. ctx.Protobuf(nil, err)
  41. return
  42. }
  43. var req pb.DisconnectReq
  44. if err = proto.Unmarshal(b, &req); err != nil {
  45. ctx.Protobuf(nil, err)
  46. return
  47. }
  48. has, err := srv.Disconnect(ctx, req.Mid, req.Key, req.Server)
  49. if err != nil {
  50. ctx.Protobuf(nil, err)
  51. return
  52. }
  53. ctx.Protobuf(&pb.DisconnectReply{Has: has}, nil)
  54. }
  55. func heartbeat(ctx *bm.Context) {
  56. query := ctx.Request.URL.Query()
  57. if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
  58. ctx.Protobuf(nil, ecode.Unauthorized)
  59. return
  60. }
  61. b, err := ioutil.ReadAll(ctx.Request.Body)
  62. if err != nil {
  63. ctx.Protobuf(nil, err)
  64. return
  65. }
  66. var req pb.HeartbeatReq
  67. if err = proto.Unmarshal(b, &req); err != nil {
  68. ctx.Protobuf(nil, err)
  69. return
  70. }
  71. if err := srv.Heartbeat(ctx, req.Mid, req.Key, req.Server); err != nil {
  72. ctx.Protobuf(nil, err)
  73. return
  74. }
  75. ctx.Protobuf(&pb.HeartbeatReply{}, nil)
  76. }
  77. func renewOnline(ctx *bm.Context) {
  78. query := ctx.Request.URL.Query()
  79. if token, err := c.Get("httpToken").String(); err != nil || token != query.Get("token") {
  80. ctx.Protobuf(nil, ecode.Unauthorized)
  81. return
  82. }
  83. b, err := ioutil.ReadAll(ctx.Request.Body)
  84. if err != nil {
  85. ctx.Protobuf(nil, err)
  86. return
  87. }
  88. var req pb.OnlineReq
  89. if err = proto.Unmarshal(b, &req); err != nil {
  90. ctx.Protobuf(nil, err)
  91. return
  92. }
  93. roomCount, err := srv.RenewOnline(ctx, req.Server, req.Sharding, req.RoomCount)
  94. if err != nil {
  95. ctx.Protobuf(nil, err)
  96. return
  97. }
  98. ctx.Protobuf(&pb.OnlineReply{RoomCount: roomCount}, nil)
  99. }