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.
 
 
 
 

61 line
1.3 KiB

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