已完成聊天交流平台的服务端消息缓存,测试缓存200条。

This commit is contained in:
dathlin
2017-06-20 11:26:40 +08:00
parent 5ff45a6e1b
commit 3a1a127f80
5 changed files with 50 additions and 14 deletions

View File

@@ -43,6 +43,7 @@
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@@ -75,7 +76,6 @@
this.panel_left = new System.Windows.Forms.Panel();
this.label10 = new System.Windows.Forms.Label();
this.panel_main = new System.Windows.Forms.Panel();
this.ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.statusStrip1.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.panel_right.SuspendLayout();
@@ -206,6 +206,13 @@
this.ToolStripMenuItem.Text = "注册账号";
this.ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
//
// 消息发送ToolStripMenuItem
//
this.ToolStripMenuItem.Name = "消息发送ToolStripMenuItem";
this.ToolStripMenuItem.Size = new System.Drawing.Size(164, 32);
this.ToolStripMenuItem.Text = "消息发送";
this.ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
//
// 设置ToolStripMenuItem
//
this.ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
@@ -519,13 +526,6 @@
this.panel_main.Size = new System.Drawing.Size(557, 419);
this.panel_main.TabIndex = 5;
//
// 消息发送ToolStripMenuItem
//
this.ToolStripMenuItem.Name = "消息发送ToolStripMenuItem";
this.ToolStripMenuItem.Size = new System.Drawing.Size(164, 32);
this.ToolStripMenuItem.Text = "消息发送";
this.ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
//
// FormMainWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

View File

@@ -331,10 +331,14 @@ namespace 软件系统客户端模版
//收到服务器的数据
JObject json = JObject.Parse(data);
UserClient.DateTimeServer = json["Time"].ToObject<DateTime>();
List<string> chats = JArray.Parse(json["chats"].ToString()).ToObject<List<string>>();
StringBuilder sb = new StringBuilder();
chats.ForEach(m => { sb.Append(m + Environment.NewLine); });
if (IsHandleCreated) Invoke(new Action(() =>
{
toolStripStatusLabel_time.Text = UserClient.DateTimeServer.ToString("yyyy-MM-dd HH:mm");
label_file_count.Text = json["FileCount"].ToObject<int>().ToString();
UIControls_Chat.AddChatsHistory(sb.ToString());
}));
}
else if (customer == CommonHeadCode.MultiNetHeadCode.)

View File

@@ -41,6 +41,14 @@ namespace 软件系统客户端模版.UIControls
{
richTextBox1.AppendText(str + Environment.NewLine);
}
/// <summary>
/// 新增聊天的历史记录
/// </summary>
/// <param name="str"></param>
public void AddChatsHistory(string str)
{
richTextBox1.Text = str;
}
public void InputFocus()
{

View File

@@ -16,6 +16,7 @@
//紧急存储数据
UserServer.ServerSettings.SaveToFile();
UserServer.ServerAccounts.SaveToFile();
Chats_Managment.SaveToFile();
if (disposing && (components != null))
{

View File

@@ -98,6 +98,8 @@ namespace 软件系统服务端模版
UserServer.ServerAccounts.FileSavePath = Application.StartupPath + @"\accounts.txt";
UserServer.ServerAccounts.LoadByFile();
UserServer.ServerAccounts.LogHelper = RuntimeLogHelper;
//初始化聊天信息
ChatInitialization();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
@@ -385,8 +387,10 @@ namespace 软件系统服务端模版
}
else if (customer == CommonHeadCode.SimplifyHeadCode.)
{
Newtonsoft.Json.Linq.JObject json = new Newtonsoft.Json.Linq.JObject();
json.Add(nameof(UserServer.ServerSettings.Announcement), new Newtonsoft.Json.Linq.JValue(UserServer.ServerSettings.Announcement));
JObject json = new JObject
{
{ nameof(UserServer.ServerSettings.Announcement), new JValue(UserServer.ServerSettings.Announcement) }
};
net_simplify_server.SendMessage(state, customer, json.ToString());
}
else if (customer == CommonHeadCode.SimplifyHeadCode.)
@@ -636,9 +640,7 @@ namespace 软件系统服务端模版
{
if (customer == CommonHeadCode.MultiNetHeadCode.)
{
string content = state.LoginAlias + DateTime.Now.ToString(" yyyy-MM-dd HH:mm:ss") + Environment.NewLine + data;
//转发所有的客户端,包括发送者
net_socket_server.SendAllClients(customer, content);
ChatAddMessage(state.LoginAlias, data);
}
}
@@ -672,13 +674,15 @@ namespace 软件系统服务端模版
JObject json = new JObject
{
{ "Time", new JValue(DateTime.Now) },
{ "FileCount", new JValue(net_simple_file_server.File_Count()) }
{ "FileCount", new JValue(net_simple_file_server.File_Count()) },
{ "chats", new JValue(Chats_Managment.ToSaveString())}
};
//发送客户端的初始化数据
net_socket_server.Send(object1, CommonHeadCode.MultiNetHeadCode., json.ToString());
//触发上下线功能
Net_socket_clients_change(DateTime.Now.ToString("MM-dd HH:mm:ss ") + object1._IpEnd_Point.Address.ToString() + "" +
object1.LoginAlias + " 上线");
ChatAddMessage(object1.LoginAlias + " 上线");
}
@@ -982,6 +986,25 @@ namespace 软件系统服务端模版
Chats_Managment.LoadByFile();//加载以前的数据
}
private void ChatAddMessage(string user,string message)
{
string content = "\u0002" + user + DateTime.Now.ToString(" yyyy-MM-dd HH:mm:ss") + Environment.NewLine + " " + message;
//转发所有的客户端,包括发送者
net_socket_server.SendAllClients(CommonHeadCode.MultiNetHeadCode., content);
//添加缓存
Chats_Managment.AddNewItem(content);
}
/// <summary>
/// 如果需要发送一些系统自己的消息,请调用这个方法。
/// 一般来说将系统消息和用户聊天消息进行区分
/// </summary>
/// <param name="message">消息</param>
private void ChatAddMessage(string message)
{
ChatAddMessage("[系统]", message);
}
#endregion