我试图制作一个WebSocket服务器,但它永远不会连接。我使用本网站作为客户
https://www.websocket.org/echo.html
在框中的那个页面上我把这个
WS://1.2.3.4:8888
1.2.3.4不是真正的IP地址,我只是把它放在这里作为占位符而不是放置我的真实IP地址。但它永远不会连接
然后下面的C#代码作为服务器,下面的代码是整个程序,但无论我做什么它从不连接,我在OnOpen中有一个断点,它永远不会碰到的C#代码,并且没有生成错误或异常。
我在防火墙中打开了端口8888,我可以使用Java中的常规套接字连接到同一个端口,并在同一台服务器上运行另一个C#程序。
有人知道它为什么不连接吗?
由于
using Fleck;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<IWebSocketConnection> sockets = new List<IWebSocketConnection>();
Fleck.WebSocketServer server = new Fleck.WebSocketServer("ws://0.0.0.0:8888");
server.Start(socket =>
{
socket.OnOpen = () =>
{
Console.WriteLine("Connection open.");
sockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine("Connection closed.");
sockets.Remove(socket);
};
socket.OnMessage = message =>
{
Console.WriteLine("Client Says: " + message);
sockets.ToList().ForEach(s => s.Send(" client says: " + message));
};
});
string input = Console.ReadLine();
while (input != "exit") {
sockets.ToList().ForEach(s => s.Send(input));
input = Console.ReadLine();
}
}
}
}
也许它与跨站点配置(CORS)等有关,或者其他一些我不知道的事情?