// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-2.1 private static async System.Threading.Tasks.Task GetTableAsync( Microsoft.AspNetCore.Http.HttpContext context , System.Net.WebSockets.WebSocket webSocket) { WebSocketTextWriter wtw = new WebSocketTextWriter(webSocket); byte[] buffer = new byte[1024 * 4]; System.Net.WebSockets.WebSocketReceiveResult result = await webSocket.ReceiveAsync( new System.ArraySegment <byte>(buffer) , System.Threading.CancellationToken.None ); SqlService service = (SqlService)context.RequestServices.GetService(typeof(SqlService)); while (!result.CloseStatus.HasValue) { //string answer = @"The server received the following message: "; //byte[] answerBuffer = System.Text.Encoding.UTF8.GetBytes(answer); //await webSocket.SendAsync( // new ArraySegment<byte>(answerBuffer, 0, answerBuffer.Length) // , System.Net.WebSockets.WebSocketMessageType.Text // , false, System.Threading.CancellationToken.None //); //wtw.WriteLine("Test 123"); //wtw.Transmit(); //await System.Threading.Tasks.Task.Delay(30); await SqlServiceJsonHelper.AnyDataReaderToJson("SELECT * FROM T_Benutzer", null, service, wtw, RenderType_t.DataTable); await wtw.TransmitAsync(); //await System.Threading.Tasks.Task.Delay(3000); //wtw.WriteLine("Test 456"); //wtw.Transmit(); //await System.Threading.Tasks.Task.Delay(30); //using (Newtonsoft.Json.JsonTextWriter jsonWriter = // new Newtonsoft.Json.JsonTextWriter(wtw)) //{ //} result = await webSocket.ReceiveAsync( new System.ArraySegment <byte>(buffer) , System.Threading.CancellationToken.None ); } // Whend await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, System.Threading.CancellationToken.None); } // End Task GetTableAsync
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-2.1 private static async System.Threading.Tasks.Task Echo( Microsoft.AspNetCore.Http.HttpContext context , System.Net.WebSockets.WebSocket webSocket) { WebSocketTextWriter wtw = new WebSocketTextWriter(webSocket); byte[] buffer = new byte[1024 * 4]; System.Net.WebSockets.WebSocketReceiveResult result = await webSocket.ReceiveAsync( new System.ArraySegment <byte>(buffer) , System.Threading.CancellationToken.None ); while (!result.CloseStatus.HasValue) { //string answer = @"The server received the following message: "; //byte[] answerBuffer = System.Text.Encoding.UTF8.GetBytes(answer); //await webSocket.SendAsync( // new ArraySegment<byte>(answerBuffer, 0, answerBuffer.Length) // , System.Net.WebSockets.WebSocketMessageType.Text // , false, System.Threading.CancellationToken.None //); wtw.WriteLine("Test 123"); await wtw.TransmitAsync(); wtw.WriteLine("Test 456"); await wtw.TransmitAsync(); wtw.WriteLine("Echo: "); await wtw.FlushAsync(); wtw.Write(@"The server received the following message: "); await wtw.FlushAsync(); // wtw.Send(false, new byte[0]); await webSocket.SendAsync( new System.ArraySegment <byte>(buffer, 0, result.Count) , result.MessageType , result.EndOfMessage , System.Threading.CancellationToken.None ); result = await webSocket.ReceiveAsync( new System.ArraySegment <byte>(buffer) , System.Threading.CancellationToken.None ); } // Whend await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, System.Threading.CancellationToken.None); } // End Task Echo
} // End Task DeserializeJSON private static async System.Threading.Tasks.Task Ransack( Microsoft.AspNetCore.Http.HttpContext context , System.Net.WebSockets.WebSocket webSocket) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); SearchArguments searchArguments = await DeserializeJSON <SearchArguments>(webSocket, serializer); string[] fieldNames = LinqHelper.GetFieldAndPropertyNames <SearchResult>(); Getter_t <SearchResult>[] getters = LinqHelper.GetGetters <SearchResult>(fieldNames); using (WebSocketTextWriter wtw = new WebSocketTextWriter(webSocket)) { #if true using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw)) { System.Threading.Tasks.Task wsa = jsonWriter.WriteStartArrayAsync(); // jsonWriter.WriteStartArray(); int j = 0; foreach (SearchResult thisSearchResult in FileSearch.SearchContent2(searchArguments)) { await wsa; // serializer.Serialize(jsonWriter, thisSearchResult); // jsonWriter.WriteStartObject(); await jsonWriter.WriteStartObjectAsync(); // jsonWriter.WritePropertyName("CharPos"); // jsonWriter.WriteValue(thisSearchResult.CharPos); // jsonWriter.WritePropertyName("File"); // jsonWriter.WriteValue(thisSearchResult.File); // jsonWriter.WritePropertyName("Line"); // jsonWriter.WriteValue(thisSearchResult.Line); // jsonWriter.WritePropertyName("LineNumber"); // jsonWriter.WriteValue(thisSearchResult.LineNumber); // jsonWriter.WritePropertyName("SearchTerm"); // jsonWriter.WriteValue(thisSearchResult.SearchTerm); /* * * await jsonWriter.WritePropertyNameAsync("CharPos"); * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos); * * await jsonWriter.WritePropertyNameAsync("CharPos"); * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos); * * await jsonWriter.WritePropertyNameAsync("File"); * await jsonWriter.WriteValueAsync(thisSearchResult.File); * * await jsonWriter.WritePropertyNameAsync("Line"); * await jsonWriter.WriteValueAsync(thisSearchResult.Line); * * await jsonWriter.WritePropertyNameAsync("LineNumber"); * await jsonWriter.WriteValueAsync(thisSearchResult.LineNumber); * * await jsonWriter.WritePropertyNameAsync("SearchTerm"); * await jsonWriter.WriteValueAsync(thisSearchResult.SearchTerm); */ for (int i = 0; i < getters.Length; ++i) { System.Threading.Tasks.Task wpnt = jsonWriter.WritePropertyNameAsync(fieldNames[i]); object value = getters[i](thisSearchResult); // if (value == System.DBNull.Value) value = null; await wpnt; await jsonWriter.WriteValueAsync(value); } // Next i // await awso; // jsonWriter.WriteEndObject(); System.Threading.Tasks.Task weo = jsonWriter.WriteEndObjectAsync(); // await weo; if (j > 0 && j % 200 == 0) { j++; await weo; await jsonWriter.WriteEndArrayAsync(); await jsonWriter.FlushAsync(); // await wtw.FlushAsync(); await wtw.SendAsync(true); await jsonWriter.WriteStartArrayAsync(); } // Next j else { j++; await weo; } } // Next thisSearchResult await jsonWriter.WriteEndArrayAsync(); //jsonWriter.WriteEndArray(); await jsonWriter.FlushAsync(); // jsonWriter.Flush(); } // End Using jsonWriter #else // System.Collections.Generic.List<SearchResult> ls = FileSearch.SearchContent(searchArguments); System.Collections.Generic.IEnumerable <SearchResult> ls = FileSearch.SearchContent2(searchArguments); using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw)) { serializer.Serialize(jsonWriter, ls); await jsonWriter.FlushAsync(); } // End Using jsonWriter #endif await wtw.SendAsync(true); } // End Using wtw await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "Normal closure; the connection successfully completed whatever purpose for which it was created.", System.Threading.CancellationToken.None); serializer = null; } // End Task Ransack