Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections.Specialized;
  2. using WebSocketSpace;
  3. namespace FunASRWSClient_Offline
  4. {
  5. /// <summary>
  6. /// /主程序入口
  7. /// </summary>
  8. public class Program
  9. {
  10. private static void Main()
  11. {
  12. WSClient_Offline m_funasrclient = new WSClient_Offline();
  13. m_funasrclient.FunASR_Main();
  14. }
  15. }
  16. public class WSClient_Offline
  17. {
  18. public static string host = "0.0.0.0";
  19. public static string port = "10095";
  20. public static string hotword = null;
  21. private static CWebSocketClient m_websocketclient = new CWebSocketClient();
  22. [STAThread]
  23. public async void FunASR_Main()
  24. {
  25. loadconfig();
  26. loadhotword();
  27. //初始化通信连接
  28. string errorStatus = string.Empty;
  29. string commstatus = ClientConnTest();
  30. if (commstatus != "通信连接成功")
  31. errorStatus = commstatus;
  32. //程序初始监测异常--报错、退出
  33. if (errorStatus != string.Empty)
  34. {
  35. //报错方式待加
  36. Environment.Exit(0);
  37. }
  38. //循环输入推理文件
  39. while (true)
  40. {
  41. Console.WriteLine("请输入转录文件路径:");
  42. string filepath = Console.ReadLine();
  43. if (filepath != string.Empty && filepath != null)
  44. {
  45. await m_websocketclient.ClientSendFileFunc(filepath);
  46. }
  47. }
  48. }
  49. private void loadconfig()
  50. {
  51. string filePath = "config.ini";
  52. NameValueCollection settings = new NameValueCollection();
  53. using (StreamReader reader = new StreamReader(filePath))
  54. {
  55. string line;
  56. while ((line = reader.ReadLine()) != null)
  57. {
  58. // 忽略空行和注释
  59. if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
  60. continue;
  61. // 解析键值对
  62. int equalsIndex = line.IndexOf('=');
  63. if (equalsIndex > 0)
  64. {
  65. string key = line.Substring(0, equalsIndex).Trim();
  66. string value = line.Substring(equalsIndex + 1).Trim();
  67. if (key == "host")
  68. host = value;
  69. else if (key == "port")
  70. port = value;
  71. }
  72. }
  73. }
  74. }
  75. static void loadhotword()
  76. {
  77. string filePath = "hotword.txt";
  78. try
  79. {
  80. // 使用 StreamReader 打开文本文件
  81. using (StreamReader sr = new StreamReader(filePath))
  82. {
  83. string line;
  84. // 逐行读取文件内容
  85. while ((line = sr.ReadLine()) != null)
  86. {
  87. hotword += line;
  88. hotword += " ";
  89. }
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. Console.WriteLine("读取文件时发生错误:" + ex.Message);
  95. }
  96. finally
  97. {
  98. if (hotword.Length > 0 && hotword[hotword.Length - 1] == ' ')
  99. hotword = hotword.Substring(0,hotword.Length - 1);
  100. }
  101. }
  102. private static string ClientConnTest()
  103. {
  104. //WebSocket连接状态监测
  105. Task<string> websocketstatus = m_websocketclient.ClientConnTest();
  106. if (websocketstatus != null && websocketstatus.Result.IndexOf("成功") == -1)
  107. return websocketstatus.Result;
  108. return "通信连接成功";
  109. }
  110. }
  111. }