System.Net.HttpListenerContext.AcceptWebSocketAsync C# (CSharp) Method

AcceptWebSocketAsync() public method

public AcceptWebSocketAsync ( string subProtocol ) : Task
subProtocol string
return Task
        public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol)
        {
            throw new PlatformNotSupportedException();
        }

Same methods

HttpListenerContext::AcceptWebSocketAsync ( string subProtocol, System.TimeSpan keepAliveInterval ) : Task
HttpListenerContext::AcceptWebSocketAsync ( string subProtocol, int receiveBufferSize, System.TimeSpan keepAliveInterval ) : Task
HttpListenerContext::AcceptWebSocketAsync ( string subProtocol, int receiveBufferSize, System.TimeSpan keepAliveInterval, ArraySegment internalBuffer ) : Task

Usage Example

		private async void HandleRequest(HttpListenerContext context)
		{
			Console.WriteLine("New Session.");
			var ws = (await context.AcceptWebSocketAsync(subProtocol: null)).WebSocket;
			clients.Add(ws);

			while (ws.State == WebSocketState.Open)
			{
				try
				{
					var buf = new ArraySegment<byte>(new byte[1024]);
					var ret = await ws.ReceiveAsync(buf, System.Threading.CancellationToken.None);

					if (ret.MessageType == WebSocketMessageType.Close)
					{
						Console.WriteLine("Session Close.");
						break;
					}
					Console.WriteLine("Got Message.");
				}
				catch
				{
					break;
				}
			}

			clients.Remove(ws);
			ws.Dispose();
		}
All Usage Examples Of System.Net.HttpListenerContext::AcceptWebSocketAsync