I’m trying to make a simple WinForms chat app with WebSockets, and I’ve got to the point where I can send data to the server from the Client, but the server cannot receive it.
Here is the server code:
public class Chat : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
Console.WriteLine(e.Data);
Send(e.Data);
}
}
static void Main(string[] args)
{
WebSocketServer wss = new WebSocketServer("ws://localhost:8080");
wss.AddWebSocketService<Chat>("/Chat");
wss.Start();
Console.WriteLine("Server started on: ws://localhost:8080/Chat!");
Console.ReadLine();
wss.Stop();
}
Here is the client code:
WebSocket ws = new WebSocket(KChat.IP);
private void chat_Load(object sender, EventArgs e)
{
ws.Connect();
ws.Send("Connected! ");
ws.OnMessage += Ws_OnMessage;
}
private void Ws_OnMessage(object? sender, MessageEventArgs e)
{
textBox1.Text = e.Data;
}
//other code
and here is the client side button, that sends the info to the server:
private void button1_Click(object sender, EventArgs e)
{
ws.Send(textBox1.Text);
textBox1.Clear();
}
Any idea why this happens?
I have tried to make it work, but I’ve got no luck.