/// <summary> /// 给客户端发送消息 /// </summary> /// <param name="client">对应的客户端</param> /// <param name="head">消息头</param> /// <param name="content">消息内容</param> public void SendMessage(Client client, byte[] content) { try { client.socket.Send(content); } catch (Exception ex) { } }
/// <summary> /// 向浏览器发送头部信息 /// </summary> /// <param name="client"></param> /// <param name="httpVersion"></param> /// <param name="mimeHeader"></param> /// <param name="len"></param> /// <param name="statusCode"></param> public void SendHeader(Client client, string httpVersion, string mimeHeader, int len, string statusCode) { TCPManager tcpManager = new TCPManager(); string value = string.Empty; if (mimeHeader.Length == 0) { mimeHeader = "text/html"; } value += httpVersion + " " + statusCode + "\r\n"; value += "Server:winwolf\r\n"; value += "Content-Type:" + mimeHeader + "\r\n"; value += "Accept-Ranges:bytes\r\n"; value += "Content-Length:" + len + "\r\n\r\n"; byte[] buf = Encoding.UTF8.GetBytes(value); tcpManager.SendMessage(client, buf); }
/// <summary> /// 发送文件到浏览器 /// 根据后缀名查询分析文件类型,后缀名不在库里的按一般文件处理,直接发送到客户端 /// </summary> /// <param name="client"></param> /// <param name="httpVersion"></param> /// <param name="mimeType"></param> /// <param name="realPath"></param> public void SendFileToBroswer(Client client, string httpVersion, string mimeType, string realPath, string rootPath) { try { FileInfo fileInfo = new FileInfo(realPath); if (!fileInfo.Exists) return; Files file = _fileList.Find((Files f) => { return f.FileExtension == fileInfo.Extension; }); if (file == null) { file = _fileList.Find((Files f) => { return f.FileExtension == Extension.OTHER; }); } file.SendFileToBroswer(client, httpVersion, mimeType, realPath, rootPath); } catch (Exception ex) { } }
/// <summary> /// 分析html文件 /// 把html中相关联的文件都发给浏览器 /// </summary> /// <param name="client"></param> /// <param name="httpVersion"></param> /// <param name="mimeType"></param> /// <param name="realPath">文件实际路径</param> /// <param name="rootPath">项目根目录路径</param> public void AnalyseHtmlFile(Client client, string httpVersion, string mimeType, string realPath, string rootPath) { List<FileItem> fileItemList = GetHtmlExtralFiles(realPath, rootPath); SendTool st = new SendTool(); //先把html文件发送到客户端 st.SendFileToBroswer(client, httpVersion, mimeType, realPath); //Thread.Sleep(50); ////把html相关文档发送到客户端 //if (fileItemList != null && fileItemList.Count > 0) //{ // foreach (FileItem fileItem in fileItemList) // { // st.SendFileToBroswer(client, httpVersion, fileItem.MimeType, fileItem.Path); // Thread.Sleep(50); // } //} }
/// <summary> /// 发送文件到浏览器 /// </summary> /// <param name="client"></param> /// <param name="realPath"></param> public void SendFileToBroswer(Client client, string httpVersion, string mimeType, string realPath) { FileStream fs = null; try { fs = new FileStream(realPath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] buf = new byte[fs.Length]; fs.Read(buf, 0, (int)fs.Length); SendHeader(client, httpVersion, mimeType, (int)fs.Length, "200"); SendToBroswer(client, buf); } catch (Exception ex) { } finally { if (fs != null) fs.Close(); } }
/// <summary> /// 发送项目起始界面 /// </summary> public void SendProjectStartFile(Client client, string httpVersion, string realPath, string rootPath) { XmlHelper xmlHelper = new XmlHelper(); XmlNode node = xmlHelper.GetServerContext(rootPath + "\\conf\\server.xml"); string docBase = node.Attributes["docBase"].Value; string file = node.Attributes["default"].Value; string path = rootPath + "\\webapps"; string temp = rootPath + "\\webapps" + Init.Instance.DocBase; DirectoryInfo dirInfo1 = new DirectoryInfo(temp); DirectoryInfo dirInfo2 = new DirectoryInfo(realPath); if (dirInfo1.Exists && dirInfo2.Exists) { if (dirInfo1.FullName == dirInfo2.FullName || dirInfo1.Parent.FullName == dirInfo2.FullName) { path += docBase; } else { path += "\\ROOT"; } } else { path += "\\ROOT"; } if (!string.IsNullOrEmpty(file)) { path += "\\" + file; } else { path += "\\index.html"; } FileManager fileManager = new FileManager(); fileManager.SendFileToBroswer(client, httpVersion, "text/html", path, rootPath); }
public MsgChangedEventArgs(byte[] content,int length,Client client) { this._content = content; this._length = length; this._client = client; }
/// <summary> /// 将信息发送给浏览器 /// </summary> /// <param name="client"></param> /// <param name="buf"></param> public void SendToBroswer(Client client, byte[] buf) { TCPManager tcpManager = new TCPManager(); tcpManager.SendMessage(client, buf); }
/// <summary> /// 将信息发送给浏览器 /// </summary> /// <param name="client"></param> /// <param name="msg"></param> public void SendToBroswer(Client client, string msg) { byte[] buf = Encoding.UTF8.GetBytes(msg); SendToBroswer(client, buf); }
/// <summary> /// 将Html文件发送到浏览器 /// </summary> /// <param name="client"></param> /// <param name="httpVersion"></param> /// <param name="mimeType"></param> /// <param name="realPath">文件实际路径</param> /// /// <param name="rootPath">项目根目录路径</param> public override void SendFileToBroswer(Client client, string httpVersion, string mimeType, string realPath, string rootPath) { AnalyseHtmlFile(client, httpVersion, mimeType, realPath, rootPath); }
/// <summary> /// 发送文件到浏览器 /// </summary> /// <param name="client"></param> /// <param name="httpVersion"></param> /// <param name="mimeType"></param> /// <param name="realPath">文件实际路径</param> /// <param name="rootPath">项目根目录路径</param> public virtual void SendFileToBroswer(Client client, string httpVersion, string mimeType, string realPath, string rootPath) { SendTool st = new SendTool(); st.SendFileToBroswer(client, httpVersion, mimeType, realPath); }