WebScoketClient.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Websocket.Client;
  2. using System.Text.Json;
  3. using System.Reactive.Linq;
  4. using FunASRWSClient_Offline;
  5. namespace WebSocketSpace
  6. {
  7. internal class CWebSocketClient
  8. {
  9. private static readonly Uri serverUri = new Uri($"ws://{WSClient_Offline.host}:{WSClient_Offline.port}"); // 你要连接的WebSocket服务器地址
  10. private static WebsocketClient client = new WebsocketClient(serverUri);
  11. public async Task<string> ClientConnTest()
  12. {
  13. string commstatus = "WebSocket通信连接失败";
  14. try
  15. {
  16. client.Name = "funasr";
  17. client.ReconnectTimeout = null;
  18. client.ReconnectionHappened.Subscribe(info =>
  19. Console.WriteLine($"Reconnection happened, type: {info.Type}, url: {client.Url}"));
  20. client.DisconnectionHappened.Subscribe(info =>
  21. Console.WriteLine($"Disconnection happened, type: {info.Type}"));
  22. client
  23. .MessageReceived
  24. .Where(msg => msg.Text != null)
  25. .Subscribe(msg =>
  26. {
  27. recmessage(msg.Text);
  28. });
  29. await client.Start();
  30. if (client.IsRunning)
  31. commstatus = "WebSocket通信连接成功";
  32. }
  33. catch (Exception ex)
  34. {
  35. Console.WriteLine(ex.ToString());
  36. client.Dispose();
  37. }
  38. return commstatus;
  39. }
  40. public async Task<Task> ClientSendFileFunc(string file_name)//文件转录
  41. {
  42. try
  43. {
  44. if (client.IsRunning)
  45. {
  46. var exitEvent = new ManualResetEvent(false);
  47. string path = Path.GetFileName(file_name);
  48. string firstbuff = string.Format("{{\"mode\": \"offline\", \"wav_name\": \"{0}\", \"is_speaking\": true}}", Path.GetFileName(file_name));
  49. client.Send(firstbuff);
  50. showWAVForm(client, file_name);
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. Console.WriteLine(ex.ToString());
  56. }
  57. return Task.CompletedTask;
  58. }
  59. public void recmessage(string message)
  60. {
  61. if (message != null)
  62. {
  63. try
  64. {
  65. JsonDocument jsonDoc = JsonDocument.Parse(message);
  66. JsonElement root = jsonDoc.RootElement;
  67. string mode = root.GetProperty("mode").GetString();
  68. string text = root.GetProperty("text").GetString();
  69. string name = root.GetProperty("wav_name").GetString();
  70. if(name == "asr_stream")
  71. Console.WriteLine($"实时识别内容: {text}");
  72. else
  73. Console.WriteLine($"文件名称:{name} 文件转录内容: {text}");
  74. }
  75. catch (JsonException ex)
  76. {
  77. Console.WriteLine("JSON 解析错误: " + ex.Message);
  78. }
  79. }
  80. }
  81. private void showWAVForm(WebsocketClient client, string file_name)
  82. {
  83. byte[] getbyte = FileToByte(file_name).Skip(44).ToArray();
  84. for (int i = 0; i < getbyte.Length; i += 1024000)
  85. {
  86. byte[] send = getbyte.Skip(i).Take(1024000).ToArray();
  87. client.Send(send);
  88. Thread.Sleep(5);
  89. }
  90. Thread.Sleep(10);
  91. client.Send("{\"is_speaking\": false}");
  92. }
  93. public byte[] FileToByte(string fileUrl)
  94. {
  95. try
  96. {
  97. using (FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read))
  98. {
  99. byte[] byteArray = new byte[fs.Length];
  100. fs.Read(byteArray, 0, byteArray.Length);
  101. return byteArray;
  102. }
  103. }
  104. catch
  105. {
  106. return null;
  107. }
  108. }
  109. }
  110. }