예제 #1
0
        public void WsTextFrameChanged(object sender, EventArgs args)
        {
            WsTextFrameEventArgs wsArgs      = (WsTextFrameEventArgs)args;
            WsConnectionDetails  details     = wsArgs.Details;
            string        message            = (string)wsArgs.Message;
            string        pathOrFileName     = details.PathOrFileName;//debug
            NetworkStream stream             = details.Stream;
            Dictionary <string, object> dict = _serializer.Deserialize <Dictionary <string, object> >(message);

            string action = (string)dict["action"];

            if (action.Equals("startRecording"))
            {
                _wsFrameWriter = new WsFrameWriter(stream);
                string groupName = (string)dict["GroupName"];
                ProcessRecordings(groupName);
            }
            else if (action.Equals("cancelProcessing"))
            {
                _processingCancelled = true;
                _recordingFinished   = true;
                Console.WriteLine("Processing Cancelled");
                Dictionary <string, object> responseDict = new Dictionary <string, object>()
                {
                    { "action", "cancelProcessing" },
                    { "message", "Processing Cancelled" }
                };
                _helpers.SendWsText(_wsFrameWriter, responseDict);
            }
        }
예제 #2
0
        private void WsTextFrameChanged(object sender, EventArgs args)
        {
            WsTextFrameEventArgs wsArgs = (WsTextFrameEventArgs)args;

            WsConnectionDetails details = wsArgs.Details;
            string message = (string)wsArgs.Message;
            //string pathOrFileName = details.PathOrFileName; //debug
            NetworkStream stream        = details.Stream;
            WsFrameWriter wsFrameWriter = new WsFrameWriter(stream);

            _requestDictionary = _serializer.Deserialize <Dictionary <string, object> >(message);
            string name = (string)_requestDictionary["name"];

            if (details.PathOrFileName == "employee")
            {
                if ((string)_requestDictionary["action"] == "add")
                {
                    int id = AddEmployee();
                    Dictionary <string, object> responseDict = new Dictionary <string, object>()
                    {
                        { "action", "add" },
                        { "message", "Added " + name + " with id " + id + " successfully." }
                    };
                    _helpers.SendWsText(wsFrameWriter, responseDict);
                }
                else if ((string)_requestDictionary["action"] == "get")
                {
                    Dictionary <string, object> employeeJson = GetEmployee();
                    Dictionary <string, object> responseDict = new Dictionary <string, object>()
                    {
                        { "action", "get" },
                        { "message", employeeJson }
                    };
                    _helpers.SendWsText(wsFrameWriter, responseDict);
                }
            }
        }
예제 #3
0
        public async Task ProcessAsync()
        {
            string header = _helpers.ReadHeader(_networkStream);

            Regex webSocketUpgradeRegex      = new Regex("Upgrade: websocket", RegexOptions.IgnoreCase);
            Match webSocketUpgradeRegexMatch = webSocketUpgradeRegex.Match(header);

            if (webSocketUpgradeRegexMatch.Success)
            {
                await PerformHandshake(header);

                string wspath = header.Split(' ')[1].TrimStart('/');
                WsConnectionDetails details = new WsConnectionDetails(wspath, _networkStream);
                _wsClients.Add(wspath, _networkStream);

                while (_tcpSocket.Connected)
                {
                    while (!_networkStream.DataAvailable)
                    {
                        ;
                    }
                    WsFrame frame = _frameReader.Read(_networkStream, _tcpSocket);
                    if (frame.OpCode == WsOpCode.ConnectionClose)
                    {
                        _tcpSocket.Close();
                        _wsClients.Remove(wspath);
                    }
                    else
                    {
                        string message            = Encoding.UTF8.GetString(frame.DecodePayload);
                        WsTextFrameEventArgs args = new WsTextFrameEventArgs(message, details);
                        _onTextFrameChanged(this, args);
                    }
                }
            }
        }
예제 #4
0
        void OnTextFrameChanged(object sender, WsTextFrameEventArgs e)
        {
            EventHandler <WsTextFrameEventArgs> handler = WsTextFrameChanged;

            handler?.Invoke(this, e);
        }