コード例 #1
0
        private void ConnectToServer()
        {
            Console.WriteLine("****************** ConnectToServer ***************");
            Regex getFileExtension = new Regex(@"\w*\.(?<extension>.+)");
            int   threadCounter    = 1;

            DisconnectSocket();
            ProgressDialog connectionDialog = new ProgressDialog();

            connectionDialog.Show();
            socket = IO.Socket("http://40.114.246.211:4200");
            socket.On(Socket.EVENT_CONNECT, (fn) =>
            {
                if (hasConnected)
                {
                    return;
                }
                hasConnected = true;
                Console.WriteLine("----------------> Connected");
                string hoyluDeviceAsJson = JsonConvert.SerializeObject(hoyluDevice);
                socket.Emit("receiverClient", hoyluDeviceAsJson);
                socket.On("device_registered", () =>
                {
                    DeviceRegistered(ref threadCounter, connectionDialog);
                });


                socket.On("getFile", (data) =>
                {
                    if (hasReceivedFile)
                    {
                        return;
                    }
                    hasReceivedFile = true;
                    Console.WriteLine("----------------> getFile");
                    System.Diagnostics.Debugger.NotifyOfCrossThreadDependency();
                    ServerFile file = JsonConvert.DeserializeObject <ServerFile>(data.ToString());
                    string url      = @"http://40.114.246.211:4200/file_for_download/:" + file.Filename;
                    byte[] lnByte;
                    lnByte = RequestFileFromServer(file, url);

                    Match match = getFileExtension.Match(file.Originalname);
                    if (match.Success)
                    {
                        string savePath = config.SaveFileToPath + "\\" + file.Originalname;

                        Dispatcher.BeginInvoke(
                            new Action(() =>
                        {
                            SaveFile(lnByte, match, savePath);
                        })
                            );
                        socket.Emit("fileReceived");
                    }
                });
            });
            socket.Connect();
        }
コード例 #2
0
        private static byte[] RequestFileFromServer(ServerFile file, string url)
        {
            byte[]         lnByte;
            HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(url);
            string         lsResponse = string.Empty;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
                {
                    lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
                    //using (FileStream stream = new FileStream(file.Filename, FileMode.Create))
                    //{
                    //    stream.Write(lnByte, 0, lnByte.Length);
                    //}
                }
                response.Close();
            }

            return(lnByte);
        }