Esempio n. 1
0
 static void receiveData()
 {
     if (clientlist.Count > 0)
     {
         for (int i = 0; i < clientlist.Count; i++)
         {
             if (clientlist[i].sClient.Available > 0)
             {
                 try {
                     string msg    = websocket.ReceiveString(clientlist[i].sClient);
                     string fin    = msg.Substring(0, 1);
                     string opcode = msg.Substring(1, 1);
                     msg = msg.Substring(2);
                     //If fin = 1 (final) and opcode = 1 (text frame)
                     if (fin == "1" && opcode == "1")
                     {
                         setClientData(i, msg);
                     }
                 } catch {
                     Console.WriteLine("[{0}] corrupted data has been received. Did client close session?");
                 }
             }
         }
     }
 }
Esempio n. 2
0
 static void receive()
 {
     if (clientlist.Count > 0)
     {
         for (int i = 0; i < clientlist.Count; i++)
         {
             if (clientlist[i].sClient.Available > 0)
             {
                 try {
                     string msg    = websocket.ReceiveString(clientlist[i].sClient);
                     string fin    = msg.Substring(0, 1);
                     string opcode = msg.Substring(1, 1);
                     msg = msg.Substring(2);
                     if (fin == "1" && opcode == "1")
                     {
                         if (clientlist[i].Authed)
                         {
                             Console.WriteLine("[{0}]: {1}", clientlist[i].Name, msg);
                             send(i, clientlist[i].Name + ": " + msg);
                         }
                         else
                         {
                             clientlist[i].setName(msg);
                             Console.WriteLine("[{0}] authenticated and changed name to [{1}]", clientlist[i].RemoteEndpoint, clientlist[i].Name);
                             send(i, clientlist[i].Name + " has connected.");
                         }
                     }
                     else if (fin == "1" && opcode == "8")
                     {
                         Console.WriteLine("[{0}] closed session.", clientlist[i].Name);
                         if (clientlist[i].Authed)
                         {
                             send(i, clientlist[i].Name + " has disconnected.");
                             clientlist[i].sClient.Close();
                             clientlist.RemoveAt(i);
                         }
                         else
                         {
                             clientlist[i].sClient.Close();
                             clientlist.RemoveAt(i);
                         }
                     }
                 } catch (Exception e) {
                     //Console.WriteLine("[{0}] corrupted data has been received. Did client close session?", clientlist[i].Name);
                     Console.WriteLine("Fail in receive method; exception: " + e.ToString());
                 }
             }
         }
     }
 }