Skip to main content
added 20 characters in body
Source Link
Philipp Sander
  • 10.3k
  • 7
  • 49
  • 80

I have got this code:

public async Task DoRespond(AspNetWebSocketContext context) { System.Net.WebSockets.WebSocket socket = context.WebSocket; while (true) { ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None); if (socket.State == WebSocketState.Open) { string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); userMessage = "Message from client : " + userMessage; buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage)); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } else { break; } } 

I need to call this async method in different class in bool method (it is an NUnit framework)

protected override bool Test() {  Websocket ws = new Websocket();  ws.ProcessRequest(context);  Thread.Sleep(1000);  logger.Write("Async method ");  var task = Task.Run(DoRespond); } 

I need to call async Task method in this bool method. How i can do that ? I aslo need call a parametre AspNetWebSocketContext context.

I have got this code:

public async Task DoRespond(AspNetWebSocketContext context) { System.Net.WebSockets.WebSocket socket = context.WebSocket; while (true) { ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None); if (socket.State == WebSocketState.Open) { string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); userMessage = "Message from client : " + userMessage; buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage)); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } else { break; } } 

I need to call this async method in different class in bool method (it is an NUnit framework)

protected override bool Test() { Websocket ws = new Websocket(); ws.ProcessRequest(context); Thread.Sleep(1000); logger.Write("Async method "); var task = Task.Run(DoRespond); } 

I need to call async Task method in this bool method. How i can do that ? I aslo need call a parametre AspNetWebSocketContext context.

I have got this code:

public async Task DoRespond(AspNetWebSocketContext context) { System.Net.WebSockets.WebSocket socket = context.WebSocket; while (true) { ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None); if (socket.State == WebSocketState.Open) { string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); userMessage = "Message from client : " + userMessage; buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage)); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } else { break; } } 

I need to call this async method in different class in bool method (it is an NUnit framework)

protected override bool Test() {  Websocket ws = new Websocket();  ws.ProcessRequest(context);  Thread.Sleep(1000);  logger.Write("Async method ");  var task = Task.Run(DoRespond); } 

I need to call async Task method in this bool method. How i can do that ? I aslo need call a parametre AspNetWebSocketContext context.

Source Link
Barpe2
  • 39
  • 1
  • 7

How I can call async method in different class?

I have got this code:

public async Task DoRespond(AspNetWebSocketContext context) { System.Net.WebSockets.WebSocket socket = context.WebSocket; while (true) { ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None); if (socket.State == WebSocketState.Open) { string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count); userMessage = "Message from client : " + userMessage; buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage)); await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); } else { break; } } 

I need to call this async method in different class in bool method (it is an NUnit framework)

protected override bool Test() { Websocket ws = new Websocket(); ws.ProcessRequest(context); Thread.Sleep(1000); logger.Write("Async method "); var task = Task.Run(DoRespond); } 

I need to call async Task method in this bool method. How i can do that ? I aslo need call a parametre AspNetWebSocketContext context.