Monday, February 1, 2010

UpLoader.cs

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/*Programmer:Engine
*PowerBy:EngineSystem
*Oicq:282602809
*Msn:Cangta2002@hotmail.com
*E-Mail:Hee_jun1985@163.com
*Authorization:Free
*/
namespace PowerTalkBox
{
///
/// 流处理中心
///

public class UpLoader
{
///
/// 输入服务器文件夹路径地址,返回一个流:WebForm
///

/// 服务器文件路径
///
public System.IO.Stream ReadFileStream(string FileName)
{
string path = HttpContext.Current.Server.MapPath(FileName);
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
///
/// 输入一个流,输出到http:WebForm
///

/// 图片流
public void OutResponse(System.IO.Stream ImageString)
{
System.IO.Stream ms = ImageString;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.CacheControl = "no-cache";
HttpContext.Current.Response.AppendHeader("Pragma", "No-Cache");
HttpContext.Current.Response.ClearContent();
// HttpContext.Current.Response.ContentType = "image/Png";
int buffersize = (int)ms.Length;
byte[] buffer = new byte[buffersize];
int count = ms.Read(buffer, 0, buffersize);
// HttpContext.Current.Response.BinaryWrite(buffer);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, count);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
///
/// 输出一个流,输出到http:WebForm
///

/// 文件路径
public void OutResponse(string FilePath)
{
FilePath = HttpContext.Current.Server.MapPath(FilePath);
string Filname = FilePath.Substring(FilePath.LastIndexOf('/') + 1);
FileInfo fi = new FileInfo(FilePath);
FileStream MyFileStream = fi.OpenRead();
long FileSize;
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddFileDependency(Filname);
HttpContext.Current.Response.BinaryWrite(Buffer);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
///
/// 把流存成文件到指定位置:WebForm
///

/// 文件流
/// 服务器文件路径
public void SaveStreamToFile(Stream FlStream, string FilePath)
{
//读
string path = HttpContext.Current.Server.MapPath(FilePath);
int buffersize = (int)FlStream.Length;
byte[] buffer = new byte[buffersize];
int count = FlStream.Read(buffer, 0, buffersize);
//写
FileInfo fi = new FileInfo(path);
FileStream fsw = fi.Create();
fsw.Write(buffer, 0, buffersize);
fsw.Close();

}
///
/// 读流进入字节:Common
///

///路径
public byte[] ReadStream(string FilePath)
{
Stream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int buffersize = (int)fs.Length;
byte[] buffer = new byte[buffersize];
int count = fs.Read(buffer, 0, buffersize);
fs.Close();
return buffer;
}
}
}

PowerTalk.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
namespace PowerTalkBox
{
///
/// PowerTalk 的摘要说明
///

public class PowerTalk
{
///
/// WWW地址
///

public static string FaceWwwPath = "";
///
/// PowerTalk
///

public PowerTalk()
{

}
#region 操作用户
///
/// 随机游客用户添加
///

public static string NewClientUserLogin(bool AutoInList)
{
string UserIDStr = "";
UserIDStr = RadomName();

while (ExistUser("游客" + UserIDStr))
{
UserIDStr = RadomName();
}
UserInfo UserItem = new UserInfo();
UserItem.UserID = "游客" + UserIDStr;
HttpRuntime.Cache.Add(UserItem.UserID, false, new AggregateCacheDependency(), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
UserItem.UserPersonInfo = UserIDStr + " 登录时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
UserItem.Level = "0";
if (AutoInList)
{
ListClass.Engin_UserList.Add(UserItem);
}
return UserItem.UserID;
}
///
/// 用户添加
///

/// 用户ID
/// 用户信息
/// 级别
public static void NewUserLogin(string UserIDStr, string UserNameStr, string Level)
{
if (ExistUser(UserIDStr))
{
throw new Exception("UserID有重复!");
}
UserInfo UserItem = new UserInfo();
UserItem.UserID = UserIDStr;
UserItem.UserPersonInfo = UserNameStr;
UserItem.Level = Level;
ListClass.Engin_UserList.Add(UserItem);
HttpRuntime.Cache.Add(UserIDStr, false, new AggregateCacheDependency(), Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(20),CacheItemPriority.Normal, null);

}
///
/// 随机起名
///

///
public static string RadomName()
{
Random rdm = new Random();
string UserIDStr = "";
for (int i = 1; i <= 5; i++)
{
UserIDStr += rdm.Next(1, 9).ToString();
}
return UserIDStr;
}
///
/// 判断有无重名
///

/// 姓名
///
public static bool ExistUser(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
return ListClass.Engin_UserList.Exists(Fc.PredicateUser);
}
///
/// 用户List列表
///

///
public static List UserInfos(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
return ListClass.Engin_UserList.FindAll(Fc.PredicateUserList);
}
///
/// 查找用户名
///

/// 用户ID
///
public static UserInfo FindUserInfo(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
UserInfo Ui = ListClass.Engin_UserList.Find(Fc.PredicateUser);
return Ui;
}

///
/// 删除用户
///

/// 用户ID
///
public static bool DeleteUserInfo(string ID)
{
HttpRuntime.Cache.Remove(ID);
UserInfo Ui = FindUserInfo(ID);
DeleteChatInfo(ID);//删除记录
return ListClass.Engin_UserList.Remove(Ui);
}
#endregion
#region 记录操作
///
/// 添加新记录:群聊
///

/// 发送者
/// 内容
public static void AddChatInfo(string _Sender, string _SendContent)
{
foreach (UserInfo Uif in ListClass.Engin_UserList)
{
ChatInfo CiItem = new ChatInfo();
CiItem.Sender = _Sender;
CiItem.Reciver = Uif.UserID;
HttpRuntime.Cache[CiItem.Reciver] = true;
CiItem.SendContent = _SendContent;
CiItem.SendTime = DateTime.Now;
ListClass.Engin_ChatList.Add(CiItem);
}
}
///
/// 添加新记录:单聊
///

/// 发送者
/// 接收者
/// 内容
public static void AddChatInfo(string _Sender, string _Reciver, string _SendContent)
{
HttpRuntime.Cache[_Reciver] = true;
ChatInfo CiItem = new ChatInfo();
CiItem.Sender = _Sender;
CiItem.SendContent = _SendContent;
CiItem.Reciver = _Reciver;
CiItem.SendTime = DateTime.Now;
ListClass.Engin_ChatList.Add(CiItem);
}
/*三种模式 1群聊,多对多 2单对多 3单对单
*1聊天室效果时
*2管理员回答问题时
*3客户问问题咨询时
*/
///
/// 读取记录
///

/// 接收者ID
///
public static List ReadChatInfo(string ID,PowerTalkBoxEnum.Enum.SystemMode MsnMd)
{
List ChatInfoArray = new List();
bool IsHaveMsg = false;
if (HttpRuntime.Cache[ID] != null)
{
if (bool.TryParse(HttpRuntime.Cache[ID].ToString(), out IsHaveMsg))
{
if (IsHaveMsg)
{



FindClass Fc = new FindClass();
Fc.UserId = ID;
ChatInfoArray = ListClass.Engin_ChatList.FindAll(Fc.PredicateChat);

foreach (ChatInfo Cif in ChatInfoArray)
{
ListClass.Engin_ChatList.Remove(Cif);
}
HttpRuntime.Cache[ID] = false;
}
}
}

return ChatInfoArray;
}
///
/// 删除记录
///

/// 接收ID
///
public static bool DeleteChatInfo(string ID)
{
FindClass Fc = new FindClass();
Fc.UserId = ID;
List ChatInfoArray = ListClass.Engin_ChatList.FindAll(Fc.PredicateChat);
foreach (ChatInfo Cif in ChatInfoArray)
{
ListClass.Engin_ChatList.Remove(Cif);
}
return true;
}

#endregion
}
///
/// 列表
///

public class ListClass
{
public static PowerTalkBoxEnum.Enum.ChatMode _MsnChatMode = PowerTalkBoxEnum.Enum.ChatMode.OneToMore;
//在线人数
public static int OnlineMsnCount=0;
private static List _Engin_UserList = new List();
private static List _Engin_ChatList = new List();
private static List _Engin_MsnList = new List();
#region 定义
//用户列表
public static List Engin_UserList
{
get
{
return _Engin_UserList;
}
set
{
_Engin_UserList = value;
}

}
//聊天记录
public static List Engin_ChatList
{
get
{
return _Engin_ChatList;
}
set
{
_Engin_ChatList = value;
}
}
//MSN记录
public static List Engin_MsnList
{

get
{
return _Engin_MsnList;
}
set
{
_Engin_MsnList = value;
}
}

#endregion
}
//51aspx
///
/// 查找类
///

public class FindClass
{
public string UserId;
public bool PredicateUser(UserInfo s)
{
if (s.UserID == UserId)
{
return true;
}
else
{
return false;
}
}

public bool PredicateUserList(UserInfo s)
{
if (s.UserID != UserId)
{
return true;
}
else
{
return false;
}
}
///
/// MSN一对一时
///

///
///
public bool PredicateMsnUser(ChatInfo c)
{
if ((c.Reciver == UserId) && (c.Sender == UserId))
{
return true;
}
else
{
return false;
}
}
public bool PredicateChat(ChatInfo c)
{
if ((c.Reciver == UserId) && (c.Sender != UserId))
{
return true;
}
else
{
return false;
}
}

}
///
/// MSN用户信息
///

public class MsnUserInfo
{
private string _userID;

private DateTime _lasttime;
public string UserID
{
get { return _userID; }
set
{
if ((value.IndexOf('$') >= 0) (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userID = value;
}
}

public DateTime LastTime
{
get { return _lasttime; }
set { _lasttime = value; }
}
}
///
/// 用户信息
///

public class UserInfo
{
private string _userID;
private string _userInfo;
private string _level;
public string UserID
{
get { return _userID; }
set
{
if ((value.IndexOf('$') >= 0) (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userID = value;
}
}
public string UserPersonInfo
{
get { return _userInfo; }
set
{
if (value.IndexOf('$') >= 0 (value.IndexOf('') >= 0))
{
throw new Exception("不能有'$',''等非法字符");
}
_userInfo = value;
}
}
public string Level
{
get { return _level; }
set { _level = value; }
}

}
///
/// 聊天暂存记录
///

public class ChatInfo
{
private string _Sender;
private string _Reciver;
private string _SendContent;
private DateTime _SendTime;
///
/// 发送者
///

public string Sender
{
set { _Sender = value; }
get { return _Sender; }
}
///
/// 接收者
///

public string Reciver
{
set { _Reciver = value; }
get { return _Reciver; }
}
///
/// 内容
///

public string SendContent
{
set { _SendContent = value; }
get { return _SendContent; }
}
///
/// 发送时间
///

public DateTime SendTime
{
set { _SendTime = value; }
get { return _SendTime; }
}
}

}

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("PowerChatRoom/Chat.aspx");
}
}

IPFindAddr.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
///
/// IPFindAddr 的摘要说明
///

public class IPFindAddr
{
///
/// 数据库联接
///

public static OleDbConnection OleConn
{
get {
return new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Jet OLEDB:Database Password=;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\App_Data\\IP.mdb"); //Access数据库连接字符串
}
}
///
/// 获取真实地址
///

public static string GetAddr
{
get {
string DiZhi = "未知网络";
OleDbConnection Conn = OleConn;
try
{
string IP = IPToInt(HttpContext.Current.Request.UserHostAddress).ToString();

Conn.Open();
object Obj = new OleDbCommand("select top 1 region from ip where IPN1<=" + IP + " and IPN2>=" + IP, Conn).ExecuteScalar();
if (Obj != null)
{
DiZhi = Obj.ToString();
}
}
catch { }
finally { Conn.Close(); }
return DiZhi;
}
}
public IPFindAddr()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///
/// IP地址和数字之间转化的算法

///

/// 传入IP地址
///

public static uint IPToInt(string ipAddress)
{
string disjunctiveStr = ".,: ";
char[] delimiter = disjunctiveStr.ToCharArray();
string[] startIP = null;
for (int i = 1; i <= 5; i++)
{
startIP = ipAddress.Split(delimiter, i);
}
string a1 = startIP[0].ToString();
string a2 = startIP[1].ToString();
string a3 = startIP[2].ToString();
string a4 = startIP[3].ToString();
uint U1 = uint.Parse(a1);
uint U2 = uint.Parse(a2);
uint U3 = uint.Parse(a3);
uint U4 = uint.Parse(a4);
uint U = U1 << 24;
U += U2 << 16;
U += U3 << 8;
U += U4;
return U;
}
///
/// 数字和IP地址之间转化的算法
///

/// 传入IP地址
///
public static string IntToIP(uint ipAddress)
{
long ui1 = ipAddress & 0xFF000000;
ui1 = ui1 >> 24;
long ui2 = ipAddress & 0x00FF0000;
ui2 = ui2 >> 16;
long ui3 = ipAddress & 0x0000FF00;
ui3 = ui3 >> 8;
long ui4 = ipAddress & 0x000000FF;
string IPstr = "";
IPstr = System.Convert.ToString(ui1) + "." + System.Convert.ToString(ui2) + "." + System.Convert.ToString(ui3) + "." + System.Convert.ToString(ui4);
return IPstr;
}
}

FetionInterFace.cs

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using PowerTalkBoxContrls.Design;
using PowerTalkBoxEnum.Enum;
using PowerTalkBox;
using System.Collections.Generic;

///
/// FetionInterFace 的摘要说明
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class FetionInterFace : System.Web.Services.WebService
{

public FetionInterFace()
{

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
///
/// 添加信息
///

/// 我的ID,一般为手机号
/// 对方ID,游客12345
/// 发送的信息
///
[WebMethod]
public void ChatAdd(string MyUserId, string ToUserID, string SendMsg)
{
PowerTalk.AddChatInfo(MyUserId, ToUserID, SendMsg);
}
///
/// 删除
///

/// 我的ID,一般为手机号
[WebMethod]
public void DeleteUserInfo(string DelUserId)
{
PowerTalkBox.PowerTalk.DeleteUserInfo(DelUserId);
}
///
/// 获取聊天内容
///

/// 我的ID,手机号码
///
[WebMethod]
public List GetChatInfos(string MyUserName)
{
List <> LCI = PowerTalk.ReadChatInfo(MyUserName, PowerTalkBoxEnum.Enum.SystemMode.WebToIm);
foreach (ChatInfo In in LCI)
{
In.SendContent = In.SendContent.Replace("

", "").Replace("

","").Replace(" "," ");
}
return LCI;
}

}