Web套接字,斑点,将消息从客户端发送到服务器

时间:2018-08-01 10:56:11

标签: c# .net websocket

*将Fleck软件包用于服务器。

我有两个单独的控制台应用程序:

Web套接字服务器:

static void Main(string[] args)   
{
  //list of client sockets
  List<IWebSocketConnection> sockets = new List<IWebSocketConnection>();

  var server = new WebSocketServer("ws://x.x.x.x:80");
  server.Start(socket =>
  {
    socket.OnOpen = () =>
    {
      Console.WriteLine("Client connected!");
      sockets.Add(socket); //add client socket to list
    };

    socket.OnClose = () =>
    {
      Console.WriteLine("Client disconnected!");
      sockets.Remove(socket); //remove client socket from list
    };

    socket.OnMessage = message =>
    {
      Console.WriteLine("Client says: " + message);
    }; 
  });

  Console.ReadLine();
}

和客户端应用:

static void Main(string[] args)
{
  ClientWebSocket client = new ClientWebSocket();
  client.ConnectAsync(new Uri("ws://10.92.84.183:80"), CancellationToken.None).Wait();
  Console.WriteLine("Client connected.");
}

如何将消息从客户端应用发送到服务器;然后让服务器通过console.writeline该消息?

此外,当客户端连接到服务器时,服务器执行socket.OnOpen(),但是当客户端关闭时,服务器不执行socket.OnClose(),有什么想法吗?

0 个答案:

没有答案