예제 #1
0
      public static InterpreterResponse[] InterpretCommands(string sID, string sIn, ref WebSessionClass SessInf)
      {
          string sin = sIn.ToLower();
          InterpreterResponse irRet = new InterpreterResponse()
          {
              SResponse = "", IsBroadcast = false, IsToSEnd = false
          };

          irRet.IsToSEnd = true; irRet.IsBroadcast = false;
          string sOP = "getdiv";

          sOP = sIn.Substring(0, sIn.IndexOf(";")).ToLower();
          switch (sOP)
          {
          case "regsession":
          {
              foreach (KeyValuePair <string, WebSessionClass> wsc in SessionDataHandler.SessionDataList)
              {
                  if (wsc.Key == sID)
                  {
                      SessInf = wsc.Value;

                      irRet.SResponse = GetSetDivString(sID, "statusDiv", "Welcome to " + SessInf.Room + " " + SessInf.SUser);
                      irRet.IsToSEnd  = true; irRet.IsBroadcast = false;
                      return(new InterpreterResponse[] { irRet });
                  }
              }
              break;
          }

          case "msgpost":
          {
              string sKey = sIn.Substring(sIn.IndexOf("<key>") + 5);
              sKey = sKey.Substring(0, sKey.IndexOf("</key>"));
              string sDiv = sIn.Substring(sIn.IndexOf("<div>") + 5);
              sDiv = sDiv.Substring(0, sDiv.IndexOf("</div>"));
              SessInf.TypingNowString += (sKey.Contains("Enter")) ? "\r\n" : sKey;
              if (sKey == "Enter")
              {
                  SessionDataHandler.SessionDataList[SessInf.sSession].sLastWords  = SessInf.TypingNowString;
                  SessionDataHandler.SessionDataList[SessInf.sSession].LastMSGDate = DateTime.Now;
                  SessInf.sLastWords = SessInf.TypingNowString;
                  InterpreterResponse ir1 = new InterpreterResponse();
                  InterpreterResponse ir2 = new InterpreterResponse();
                  ir1.SResponse   = GetSetDivString("*", sDiv, "");        // SessInf.TypingNowString);
                  ir1.IsBroadcast = false; ir1.IsToSEnd = true;

                  ir2.SResponse   = GetAddToDivString("*", "mainPageDiv", "<span style=\"background-color:green\">" + SessInf.SUser + "</span>:<div style=\"background-color:blue;\">" + SessInf.TypingNowString.Replace("\r\n", "<br />") + "</div>");
                  ir2.IsRoomCast  = true;
                  ir2.sRoomTo     = SessInf.Room;
                  ir2.IsBroadcast = true; ir2.IsToSEnd = true;
                  if (sKey == "Enter")
                  {
                      SessInf.TypingNowString = "";
                  }
                  return(new InterpreterResponse[] { ir1, ir2 });
              }
              if (sKey == "ShiftEnter")
              {
                  SessInf.TypingNowString += "\r\n";
                  InterpreterResponse ir = new InterpreterResponse()
                  {
                      IsBroadcast = false,
                      IsToSEnd    = true,
                      IsRoomCast  = false
                  };
                  string sKeys = GetSetDivString("*", sDiv, SessInf.TypingNowString.Replace("\r\n", "<br />"));
                  return(new InterpreterResponse[] { irRet });
              }


              if (sKey == "Enter")
              {
                  SessInf.TypingNowString = "";
                  return(new InterpreterResponse[] { irRet });
              }

              if (!sKey.Contains("Enter"))
              {
                  irRet.IsBroadcast = false;
                  irRet.IsToSEnd    = true;
                  string sKeys = GetSetDivString("*", sDiv, SessInf.TypingNowString);
                  return(new InterpreterResponse[] { irRet });
              }
              break;
          }

          case "getdiv":
          {
              try
              {
                  string sDiv = sIn.Substring(0, sIn.IndexOf("</div>"));
                  sDiv            = sDiv.Substring(sDiv.ToLower().IndexOf("<div>") + 5);
                  irRet.SResponse = GetSetDivString(sID, sDiv, "User: "******".");
                  irRet.IsToSEnd  = true; irRet.IsBroadcast = false;
                  return(new InterpreterResponse[] { irRet });
              }
              catch (Exception e)
              {
                  ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "GetDiv Error;\r\n");
                  return(new InterpreterResponse[] { });
              }
          }
          }
          return(new InterpreterResponse[] { irRet });
      }
예제 #2
0
        async public void DoWSServeSocket(WSServerTaskAndOject wssto)
        {
            CancellationToken       ct  = wssto.ct;
            CancellationTokenSource cts = wssto.wsCts;

            NSXUserObj.sIP           = hlcIO.Request.RemoteEndPoint.Address.ToString();
            NSXUserObj.sUserAgent    = hlcIO.Request.UserAgent;
            NSXUserObj.DateStarted   = DateTime.Now;
            NSXUserObj.DateOfLastMsg = DateTime.Now;
            try
            {
                //hlcIO.Response.SendChunked = true;
                wsc = await hlcIO.AcceptWebSocketAsync("Neals");
            }
            catch (WebSocketException we)
            {
                isOKReading = false; IsNeeded = false;
                return;
            }
            WebSocketReceiveResult wr = null;

            while (isOKReading && !ct.IsCancellationRequested)
            {
                byte[] bWsIn = new byte[1024];

                try
                {
                    wr = await wsc.WebSocket.ReceiveAsync(new ArraySegment <byte>(bWsIn), wsso.ct);
                }
                catch
                {
                    isOKReading = false; IsNeeded = false;
                    cts.Cancel();
                    wssto.wsioh.Dispose();
                    IsNeeded = false;
                    ctsServerThread.Cancel();
                    wssto.wsioh.Dispose();
                    return;
                }
                if (wr.MessageType == WebSocketMessageType.Close)
                {
                    isOKReading = false; IsNeeded = false;
                }
                if (wr.MessageType == WebSocketMessageType.Text)
                {
////                    ChatServerTypes.UpdateTextSafe(ParentCW.Frm.textBox1, "Starting Server \r\n");

                    string sIN = Encoding.ASCII.GetString(bWsIn, 0, wr.Count);
                    //ChatServerTypes.UpdateTextSafe(ParentCW.Frm.textBox1, sIN + "\r\n");
                    string sHeads = "<textarea style=\"height:200px; width:200px;\">";

                    foreach (string hrh in hlcIO.Request.Headers)
                    {
                        sHeads += hrh + "\\" + hlcIO.Request.Headers[hrh] + "\r\n";
                    }
                    sHeads += "</textarea>\r\n";
                    bInBuff = null;

                    byte[] b = Encoding.ASCII.GetBytes(sHeads);

                    //int iLen; int iExtras;
                    //Int32[] b32 = StringByteInt.BytesToInt32s(b, out iLen, out iExtras);
                    //StringByteInt.minusFromAll(ref b32, 127);

                    //StringByteInt.plusToAll(ref b32, 127);

                    //b = StringByteInt.Int32sToBytes(b32, iLen, iExtras);
                    string sss = Encoding.ASCII.GetString(b, 0, b.Count());
                    bool   isBroadcast; bool isToSend;

                    string sData = sIN.Substring(0, sIN.IndexOf("</data>"));
                    sData = sData.Substring(sData.IndexOf("<data>") + 6);

                    string sid = sIN.Substring(sIN.IndexOf("<sid>") + 5).ToLower();
                    sid = sid.Substring(0, sid.IndexOf("</sid>")).ToLower();

                    sID = sid;
                    try
                    {
                        SessionInfo = SessionDataHandler.SessionDataList[sid];
                    }
                    catch { SessionInfo = new WebSessionClass(); }

                    //ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "Session not matching on WS request!");

                    InboundMessageParser.InterpreterResponse[] sRet = InboundMessageParser.InterpretCommands(sID, sData, ref SessionInfo);
                    foreach (InboundMessageParser.InterpreterResponse irMSG in sRet)
                    {
                        if (irMSG.IsToSEnd && irMSG.SResponse != "")
                        {
                            if (irMSG.IsRoomCast)
                            {
                                ParentCW.SendToRoom(irMSG.sRoomTo, irMSG.SResponse);
                            }
                            else if (irMSG.IsBroadcast)
                            {
                                ParentCW.SendToAll(irMSG.SResponse);
                            }
                            else
                            {
                                AddToWriteList(Encoding.ASCII.GetBytes(irMSG.SResponse));
                            }
                        }
                    }
                    //t = Task.Run(() => DoSomeWork(1, token), token);
                }
            }
            ctsServerThread.Cancel();
            wssto.wsioh.Dispose();


            string s = ctsServerThread.Token.CanBeCanceled.ToString();
        }