示例#1
0
        private void SendFileList(IWorkSocketClient client, FileMessageType type)
        {
            WebSocketMessageBuilder builder = WebSocketMessageBuilder.GetMessage(MessageType.FILELIST);
            DirectoryInfo           info    = new DirectoryInfo(Program.FILE_STORE_PATH);

            FileInfo[] files = info.GetFiles();
            builder.SetFileList(from f in info.GetFiles() select f.Name);
            String2 message = builder.Build();

            if (type == FileMessageType.FileSearch)
            {
                client.Send((int)Opcode.BINARY, message);

                /*if (!clientlist.Contains(this))
                 * {
                 *  clientlist.Add(this);
                 * }*/
            }
            else if (type == FileMessageType.FileListNotice)
            {
                /*foreach (WebSocketServer client in clientlist)
                 * {
                 *  client.Send((int)OPCODE.BINARY, message);
                 * }*/
            }
        }
示例#2
0
        private void SendWorkTemp(IWorkSocketClient client, String file, String title)
        {
            WebSocketMessageBuilder builder = WebSocketMessageBuilder.GetMessage(MessageType.WORKTEMP);
            FileInfo info = new FileInfo(Program.WORK_PATH + Path.DirectorySeparatorChar + file);
            String2  data = new String2((int)info.Length, Encoding.UTF8);

            using (FileStream stream = new FileStream(info.FullName, FileMode.Open, FileAccess.Read))
            {
                stream.Read(data.ToBytes(), 0, data.Length);
                //data = String2.ReadStream(stream, Encoding.UTF8, (int)info.Length);
            }
            builder.SetWorkTitle(title);
            builder.SetMessage(data.ToString());
            String2 message = builder.Build();

            client.Send((int)Opcode.BINARY, message);
        }
示例#3
0
        private void SendWorkList(IWorkSocketClient client, WorkType type)
        {
            WebSocketMessageBuilder builder = WebSocketMessageBuilder.GetMessage(MessageType.WORKLIST);
            DirectoryInfo           info    = new DirectoryInfo(Program.WORK_PATH);

            FileInfo[] files = info.GetFiles();
            builder.SetFileList(from f in info.GetFiles() where !String.Equals(f.Name, "default") select f.Name);
            String2 message = builder.Build();

            if (type == WorkType.WorkListNotice)
            {
                foreach (IWorkSocketClient c in WorkSocketFactory.GetWorkSocketServer().GetSocketList())
                {
                    c.Send((int)Opcode.BINARY, message);
                }
            }
            else if (type == WorkType.WorkSearch)
            {
                client.Send((int)Opcode.BINARY, message);
            }
        }
示例#4
0
        public static void Run(IWorkSocketClient client, byte opcode, String2 data)
        {
            SendWorkTemp(client, "default", DateTime.Now.ToString("yyyy_MM_dd") + "_業務報告");
            SendFileList(client, FileMessageType.FileSearch);
            SendWorkList(client, WorkType.WorkSearch);

            if (file.Open && opcode != (int)Opcode.BINARY)
            {
                //logger.Error("It's error what transfer the file.");
                file.Init();
            }
            if (opcode == (int)Opcode.MESSAGE)
            {
                IDictionary <String, String> messageBuffer = JsonConvert.DeserializeObject <Dictionary <String, String> >(data.ToString());
                if (String.Equals(messageBuffer["TYPE"], "1"))
                {
                    WebSocketMessageBuilder builder = WebSocketMessageBuilder.GetMessage(MessageType.MESSAGE);
                    String chatMessage = client.SocketClient.RemoteEndPoint + "-" + messageBuffer["MESSAGE"];
                    builder.SetMessage(chatMessage);
                    String2 message = builder.Build();
                    Console.WriteLine(message);

                    /*foreach (WebSocketServer client1 in clientlist)
                     * {
                     *  client1.Send((int)OPCODE.MESSAGE, message);
                     * }*/
                    //logger.Info(message);
                }
                else if (String.Equals(messageBuffer["TYPE"], "4"))
                {
                    FileInfo info  = new FileInfo(Program.WORK_PATH + Path.DirectorySeparatorChar + messageBuffer["WORKTITLE"]);
                    String2  data1 = new String2(messageBuffer["MESSAGE"], Encoding.UTF8);
                    using (FileStream stream = new FileStream(info.FullName, FileMode.Create, FileAccess.Write))
                    {
                        //data1.WriteStream(stream);
                        stream.Write(data1.ToBytes(), 0, data1.Length);
                    }
                    SendWorkList(client, WorkType.WorkListNotice);
                }
                else if (String.Equals(messageBuffer["TYPE"], "5"))
                {
                    String data1 = messageBuffer["MESSAGE"];
                    SendWorkTemp(client, data1.Trim(), data1.Trim());
                }
            }
            if (opcode == (int)Opcode.BINARY)
            {
                if (data.Length < 1)
                {
                    //logger.Error("It is being have downloading.but because what the data is nothing is stopped.");
                    //continue;
                }
                byte type = data[0];
                if (type == (byte)FileMessageType.FileOpen)
                {
                    file.Length = BitConverter.ToInt32(data.ToBytes(), 1);
                    String2 filename = data.SubString(5, data.Length - 5);
                    filename.Encode = Encoding.UTF8;
                    //logger.Info("filename - " + filename);
                    file.SetStream(new FileStream(Program.FILE_STORE_PATH + filename.Trim().ToString(), FileMode.Create, FileAccess.Write), file.Length);
                    //continue;
                }
                if (type == (byte)FileMessageType.FileWrite)
                {
                    if (!file.Open)
                    {
                        //logger.Error("It is being have downloading.but because what file's connection is closed.");
                        file.Init();
                        //continue;
                    }
                    String2 binary = data.SubString(1, data.Length - 1);
                    file.StreamBuffer.Write(binary.ToBytes(), 0, binary.Length);
                    file.Peek += binary.Length;
                    //logger.Info(file.Peek);
                    if (file.Peek >= file.Length)
                    {
                        file.Complete();
                        client.Send((int)Opcode.BINARY, new String2("File upload Success!!", Encoding.UTF8));
                    }
                    //continue;
                }
                if (type == (byte)FileMessageType.FileSearch || type == (byte)FileMessageType.FileListNotice)
                {
                    SendFileList(client, (FileMessageType)type);
                    //continue;
                }
                if (type == (byte)WorkType.WorkSearch || type == (byte)WorkType.WorkListNotice)
                {
                    SendWorkList(client, (WorkType)type);
                }
                //logger.Error("FileMessage type is wrong.");
                file.Init();
            }
        }