Esempio n. 1
0
        static void Main(string[] args)
        {
            if (!appServer.Setup(serverConfig)) //Setup the appServer
            {
                Console.WriteLine("开启服务器失败");
                return;
            }

            if (!appServer.Start())//Try to start the appServer
            {
                Console.WriteLine("开启服务器失败");
                return;
            }
            //注册事件
            appServer.NewSessionConnected += appServer_NewSessionConnected; //客户端连接
            appServer.NewMessageReceived  += appServer_NewMessageReceived;  //客户端接收消息
            appServer.SessionClosed       += appServer_SessionClosed;       //客户端关闭

            while (true)
            {
                Console.WriteLine("服务器开启成功!");
                Console.WriteLine();
                Console.WriteLine("1:退出");
                Console.WriteLine("Other:发送数据");
                string temp = Console.ReadLine();
                if (temp == "1")
                {
                    appServer.Stop();
                    break;
                }
                else
                {
                    foreach (var item in clients)
                    {
                        ((WebSocketSession)item.Value).Send(temp);
                    }
                }
            }
        }
Esempio n. 2
0
 static void Main(string[] args)
 {
     try
     {
         var                         contador            = 0;
         IPAddress[]                 IpLocal             = Dns.GetHostAddresses(Dns.GetHostName());
         string                      ipServidorWebSocket = "ws://192.168.137.1:9898";                //definimos la ip del equipo server
         WebSocketServer             servidorSocket      = new WebSocketServer(ipServidorWebSocket); //instanciamos el servidor
         List <IWebSocketConnection> clientesSockets     = new List <IWebSocketConnection>();        //lista de clientes conectados
         Console.WriteLine("Servidor iniciado [" + Convert.ToString(IpLocal[1]) + "]" + DateTime.Now.ToString("dd/MM/yyyy"));
         servidorSocket.Start(clientesSocket =>
         {
             clientesSocket.OnOpen = () =>//cuando alguien se conecta
             {
                 clientesSockets.Add(clientesSocket);
                 Console.WriteLine("Hay " + clientesSockets.Count() + " conectados");
             };
             clientesSocket.OnMessage = (string texto) =>//cuando se envia algo al server
             {
                 contador++;
                 clientesSockets.ForEach(p => p.Send(texto));
                 Console.WriteLine(contador.ToString() + " " + texto + " hora: " + DateTime.Now.ToString("HH:mm:ss"));
             };
             clientesSocket.OnClose = () =>//sale
             {
                 clientesSockets.Remove(clientesSocket);
                 Console.WriteLine("Cliente desconectado");
             };
         });
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: " + ex.Message);
     }
     Console.ReadKey();
 }