I have created a .NET Console App
in which i want to accept client
websockets.(I am using the chrome extension -Simple Web Socket Client
).
When i try to send messages to the connected clients i get this error in the browser :
WebSocket connection to 'ws://localhost:8600/' failed:
Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
CLOSED: ws://localhost:8600
Server
class Program
{
static async Task Main(string[] args)
{
CancellationTokenSource src = new CancellationTokenSource();
Console.WriteLine("starting server...");
Task server = Task.Run(async () =>
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8600);
serverSocket.Bind(point);
Console.WriteLine("Listening on localhost:8600" );
serverSocket.Listen(6);
var client = await serverSocket.AcceptAsync();
Task clientTask = Task.Run(async () =>
{
while (true)
{
await client.SendAsync(Encoding.UTF8.GetBytes("Date is" + DateTime.Now.ToString()), SocketFlags.None);
await Task.Delay(5000);
}
},src.Token);
}, src.Token);
while (true)
{
if (Console.ReadKey().Key == ConsoleKey.A)
{
src.Cancel();
break;
}
}
}
}
From Simple Web Socket Client ( chrome extension) i try to connect to :
ws://localhost:8600
P.S I am not sure about passing the same token in all tasks , but besides this i do not know why it keeps with this error.