Пример #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
        public BasicWebServer(string baseFolderPath = "", int?tcpPort = 54001, string httpPrefix = "http://localhost:8080/")
        {
            _tcpPort     = tcpPort;
            _httpPrefix  = httpPrefix;
            _wsClients   = new Dictionary <string, NetworkStream>();
            _httpMethods = new string[4] {
                "get", "post", "put", "delete"
            };
            _cts = new CancellationTokenSource();
            _ct  = _cts.Token;
            _ct.Register(() => {
                _httpListener.Stop();
                if (_tcpPort != null)
                {
                    _tcpListener.Stop();

                    if (_tcpSocket != null && _tcpSocket.Connected)
                    {
                        //if we have active web socket clients send them a close message
                        foreach (KeyValuePair <string, NetworkStream> entry in _wsClients)
                        {
                            WsFrameWriter frameWriter = new WsFrameWriter(entry.Value);
                            //set the close reason to Normal
                            BinaryReaderWriter.WriteUShort((ushort)WebSocketCloseCode.Normal, entry.Value, false);
                            frameWriter.Write(WsOpCode.ConnectionClose, new byte[1], true);
                        }
                        _tcpSocket.Shutdown(SocketShutdown.Both);
                        _tcpSocket.Close();
                    }
                }
                Console.WriteLine("Cancellation has been requested.");
            });

            //check baseFolderPath
            if (!Directory.Exists(baseFolderPath))
            {
                Console.WriteLine(baseFolderPath + " does not exist.");
                Console.WriteLine("Do you want to create it? (y/n)");
                ConsoleKeyInfo consoleKey = Console.ReadKey();
                if (consoleKey.KeyChar == 'y')
                {
                    Directory.CreateDirectory(baseFolderPath);
                    Console.WriteLine();
                    Console.WriteLine("Created base folder " + baseFolderPath);
                    Console.WriteLine("Press any key to exit server.");
                    Console.ReadKey();
                }
            }
            else
            {
                _baseFolderPath = baseFolderPath;
            }
        }
Пример #3
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);
                }
            }
        }
Пример #4
0
        public void SendWsText(WsFrameWriter wsFrameWriter, Dictionary <string, object> responseDict)
        {
            string responseJson = _serializer.Serialize(responseDict);

            wsFrameWriter.Write(WsOpCode.TextFrame, Encoding.UTF8.GetBytes(responseJson), true);
        }