golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
преди 2 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "context"
  4. "gim/config"
  5. "gim/internal/connect"
  6. "gim/pkg/db"
  7. "gim/pkg/interceptor"
  8. "gim/pkg/logger"
  9. "gim/pkg/pb"
  10. "gim/pkg/rpc"
  11. "google.golang.org/grpc"
  12. "net"
  13. "os"
  14. "os/signal"
  15. "syscall"
  16. "go.uber.org/zap"
  17. )
  18. func main() {
  19. config.Init()
  20. db.Init()
  21. // 启动TCP长链接服务器
  22. go func() {
  23. connect.StartTCPServer(config.TCPListenAddr)
  24. }()
  25. // 启动WebSocket长链接服务器
  26. go func() {
  27. connect.StartWSServer(config.WSListenAddr)
  28. }()
  29. // 启动服务订阅
  30. connect.StartSubscribe()
  31. server := grpc.NewServer(grpc.UnaryInterceptor(interceptor.NewInterceptor("connect_interceptor", nil)))
  32. // 监听服务关闭信号,服务平滑重启
  33. go func() {
  34. c := make(chan os.Signal, 1)
  35. signal.Notify(c, syscall.SIGTERM)
  36. s := <-c
  37. logger.Logger.Info("server stop start", zap.Any("signal", s))
  38. _, _ = rpc.GetLogicIntClient().ServerStop(context.TODO(), &pb.ServerStopReq{ConnAddr: config.LocalAddr})
  39. logger.Logger.Info("server stop end")
  40. server.GracefulStop()
  41. }()
  42. pb.RegisterConnectIntServer(server, &connect.ConnIntServer{})
  43. listener, err := net.Listen("tcp", config.RPCListenAddr)
  44. if err != nil {
  45. panic(err)
  46. }
  47. logger.Logger.Info("rpc服务已经开启")
  48. err = server.Serve(listener)
  49. if err != nil {
  50. logger.Logger.Error("serve error", zap.Error(err))
  51. }
  52. }