diff --git a/Interact/AttrNames.cs b/Interact/AttrNames.cs
index a780338..1d9fa81 100644
--- a/Interact/AttrNames.cs
+++ b/Interact/AttrNames.cs
@@ -8,10 +8,14 @@ namespace Interact
//Json数据的字段访问字符串
public static class AttrNames
{
+ //基本信息
public const string Token = "Token";
public const string Operation = "Operation";
- public const string Error = "Error";
+ //处理结果信息
+ public const string Error = "Error";
+
+ //用户信息
public const string User = "User";
public const string NickName = "NickName";
public const string Motto = "Motto";
diff --git a/Interact/Client.cs b/Interact/Client.cs
index 3ed99bc..c2de87a 100644
--- a/Interact/Client.cs
+++ b/Interact/Client.cs
@@ -7,6 +7,7 @@ using System.Net;
using System.Threading;
using System.Collections.Concurrent;
using System.Drawing;
+using System.IO;
namespace Interact
{
@@ -46,7 +47,7 @@ namespace Interact
//获取用户列表结果处理事件
public static event GetUserListDoneHandler OnGetUserListDone;
//获取用户头像结果处理事件
- public static event GetUserPhotoDoneHandler OnGetUserPhotoDoneHandler;
+ public static event GetUserPhotoDoneHandler OnGetUserPhotoDone;
//发送消息时是否要求服务器返回处理结果
public static bool DoesSendMessageReturn
{
@@ -78,7 +79,8 @@ namespace Interact
}
///
- /// 初始化客户端,连接服务器。如果成功则启动数据接收和发送线程
+ /// 初始化客户端,连接服务器。如果成功则启动数据接收和发送线程。
+ /// 注意,当网络不稳定时此方法可能会阻塞。
///
/// 如果已连接到服务器返回true,否则返回false。
public static bool Initialize()
@@ -135,31 +137,100 @@ namespace Interact
/// 数据发送完毕时回调函数
/// 成功将请求加入发送队列返回true,否则返回false。
public static bool QueueSendMessage(string text, User to, Action callback = null)
- { return false; }
+ {
+ JsonSendMessageHead jsonObj = new JsonSendMessageHead()
+ {
+ Token = "",
+ Operation = Operations.SendMessage.ToString(),
+ To = to.Name,
+ NeedResult = DoesSendMessageReturn ? "1" : "0"
+ };
+ Packet packet = new Packet
+ {
+ Head = jsonObj,
+ Data = Encoding.UTF8.GetBytes(text),
+ CallBack = callback
+ };
+ return Send(packet);
+ }
+
///
/// 请求登出。异步,此方法将请求放入请求队列后返回。
///
/// 数据发送完毕时回调函数
/// 成功将请求加入发送队列返回true,否则返回false。
-
public static bool QueueLogout(Action callback = null)
- { return false; }
+ {
+ BaseHead jsonObj = new BaseHead()
+ {
+ Token = "",
+ Operation = Operations.Logout.ToString(),
+ };
+ Packet packet = new Packet
+ {
+ Head = jsonObj,
+ Data = null,
+ CallBack = callback
+ };
+ return Send(packet);
+ }
+
///
/// 请求用户列表。异步,此方法将请求放入请求队列后返回。
///
/// 数据发送完毕时回调函数
/// 成功将请求加入发送队列返回true,否则返回false。
-
public static bool QueueGetUserList(Action callback = null)
- { return false; }
+ {
+ BaseHead jsonObj = new BaseHead()
+ {
+ Token = "",
+ Operation = Operations.GetUserList.ToString(),
+ };
+ Packet packet = new Packet
+ {
+ Head = jsonObj,
+ Data = null,
+ CallBack = callback
+ };
+ return Send(packet);
+ }
+
///
/// 请求更新当前登录用户信息。异步,此方法将请求放入请求队列后返回。
///
/// 数据发送完毕时回调函数
/// 成功将请求加入发送队列返回true,否则返回false。
-
- public static bool QueueUpdateUserInfo(Action callback = null)
- { return false; }
+ public static bool QueueUpdateUserInfo(UserInfo userInfo, Action callback = null)
+ {
+ byte[] photoData = null; //头像数据
+ //将头像数据转换到字符数组
+ if (userInfo.Photo != null)
+ {
+ MemoryStream memStream = new MemoryStream();
+ userInfo.Photo.Save(memStream, userInfo.Photo.RawFormat);
+ photoData = new byte[memStream.Length];
+ memStream.Seek(0, SeekOrigin.Begin);
+ memStream.Read(photoData, 0, photoData.Length);
+ }
+ //构建数据包
+ JsonUserInfoHead jsonObj = new JsonUserInfoHead()
+ {
+ Token = "",
+ Operation = Operations.UpdateUserInfo.ToString(),
+ NickName = userInfo.NickName,
+ Password = userInfo.Password,
+ Motto = userInfo.Motto,
+ Photo = userInfo.Photo == null ? 0 : photoData.Length
+ };
+ Packet packet = new Packet
+ {
+ Head = jsonObj,
+ Data = photoData,
+ CallBack = callback
+ };
+ return Send(packet);
+ }
///
/// 请求获取指定用户的头像。异步,此方法将请求放入请求队列后返回。
@@ -168,6 +239,20 @@ namespace Interact
/// 数据发送完毕时回调函数
/// 成功将请求加入发送队列返回true,否则返回false。
public static bool QueueGetUserPhoto(User user, Action callback = null)
- { return false; }
+ {
+ JsonGetUserPhotoHead jsonObj = new JsonGetUserPhotoHead()
+ {
+ Token = "",
+ Operation = Operations.GetUserPhoto.ToString(),
+ User = user.Name
+ };
+ Packet packet = new Packet
+ {
+ Head = jsonObj,
+ Data = null,
+ CallBack = callback
+ };
+ return Send(packet);
+ }
}
}
diff --git a/Interact/Handle.cs b/Interact/Handle.cs
index b2871e3..7b29bcc 100644
--- a/Interact/Handle.cs
+++ b/Interact/Handle.cs
@@ -50,7 +50,10 @@ namespace Interact
{
//发起登录完成事件
if (head.Error != "")
+ {
OnLoginDone?.Invoke(head, null);
+ return;
+ }
JObject dataObj = (JObject)JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data));
User user = new User
{
diff --git a/Interact/JsonObject.cs b/Interact/JsonObject.cs
index fd2ef53..c8602a5 100644
--- a/Interact/JsonObject.cs
+++ b/Interact/JsonObject.cs
@@ -10,6 +10,7 @@ namespace Interact
public class BaseHead
{
//请求ID,由发送方定义的随机字符串。如果接受方有返回数据应包含相同的Token
+ //此特性暂时未启用,传空字符串即可
public string Token;
//操作类型,决定数据的其他内容
public string Operation;
@@ -30,13 +31,26 @@ namespace Interact
public string Pwd;
}
- //用户信息
- internal class JsonUserInfo
+ //发送消息包头
+ internal class JsonSendMessageHead : BaseHead
{
- public string User; //用户名
- public string NickName; //昵称
- public string Motto; //个性签名
- public string UGroup; //用户组
+ public string To; //消息接收者用户名
+ public string NeedResult = "0"; //是否要求服务器返回处理结果。否传“0”
+ }
+
+ //更新用户信息包头
+ internal class JsonUserInfoHead : BaseHead
+ {
+ public string NickName = null;
+ public string Password = null;
+ public string Motto = null;
+ public int Photo = 0; //新头像大小
+ }
+
+ //获取用户头像请求的包头
+ internal class JsonGetUserPhotoHead : BaseHead
+ {
+ public string User; //要获取头像的用户名
}
#endregion
}
diff --git a/Interact/User.cs b/Interact/User.cs
index f8ad5db..053ad61 100644
--- a/Interact/User.cs
+++ b/Interact/User.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.Drawing;
namespace Interact
{
@@ -17,6 +18,15 @@ namespace Interact
public UserGroup Group; //用户组
}
+ //更新用户信息时新的用户信息。如果项值为null则不更改相应的信息
+ public class UserInfo
+ {
+ public string Password = null; //新密码
+ public string NickName = null; //新昵称
+ public string Motto = null; //新签名
+ public Image Photo = null; //新头像
+ }
+
//用户类型
public enum UserGroup
{
diff --git a/StormChatWPF/App.xaml b/StormChatWPF/App.xaml
index 3292a54..1fc1979 100644
--- a/StormChatWPF/App.xaml
+++ b/StormChatWPF/App.xaml
@@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:StormChatWPF"
- StartupUri="MainWindow.xaml">
+ StartupUri="LogWindow.xaml">
diff --git a/StormChatWPF/Chat.cs b/StormChatWPF/Chat.cs
new file mode 100644
index 0000000..6377be9
--- /dev/null
+++ b/StormChatWPF/Chat.cs
@@ -0,0 +1,69 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Interact;
+using System.Drawing;
+using System.Windows;
+
+namespace StormChatWPF
+{
+ class Chat
+ {
+ public static List userlist = new List();//联系人集合
+
+
+ internal static void GetUsers(ResultHandler head, User user)
+ {
+
+ }//获取联系人对象
+ internal static void GetUserList(ResultHead head, User[] users)
+ {
+ if (head.Error != "")
+ {
+ MessageBox.Show("无法获取联系人列表");
+ }
+ else
+ return;
+ }//获取用户列表
+ internal static void GetUserPhoto(ResultHead head, Image image)
+ {
+ if (head.Error != "")
+ {
+ MessageBox.Show("无法读取用户图片");
+ }
+ }//对用户头像进行赋值
+ internal void Log(string user,string password)
+ {
+ if (StormClient.Initialize())
+ {
+ StormClient.QueueLogin(user,password);
+ }
+ else
+ {
+ MessageBox.Show("连接服务器失败!");
+ }
+ }//登录
+ internal static void HaveLogin(ResultHead head, User user)
+ {
+ App.Current.Dispatcher.Invoke(
+ (Action)delegate ()
+ {
+ if (head.Error == "")
+ {
+ User.Me = user;
+ MainWindow userWindow = new MainWindow();
+ userWindow.Show();
+ LogWindow.a.Close();
+ }
+ else
+ {
+ MessageBox.Show("登录失败!");
+ }
+ });
+ }//登录完成
+
+
+ }
+}
diff --git a/StormChatWPF/LogWindow.xaml b/StormChatWPF/LogWindow.xaml
new file mode 100644
index 0000000..0e46e9f
--- /dev/null
+++ b/StormChatWPF/LogWindow.xaml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/StormChatWPF/LogWindow.xaml.cs b/StormChatWPF/LogWindow.xaml.cs
new file mode 100644
index 0000000..2775cfa
--- /dev/null
+++ b/StormChatWPF/LogWindow.xaml.cs
@@ -0,0 +1,40 @@
+using Interact;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace StormChatWPF
+{
+ ///
+ /// MainWindow.xaml 的交互逻辑
+ ///
+ public partial class LogWindow : Window
+ {
+ public static LogWindow a;
+ public LogWindow()
+ {
+ InitializeComponent();
+ StormClient.OnLoginDone += Chat.HaveLogin;
+ StormClient.OnGetUserListDone += Chat.GetUserList;
+ StormClient.OnGetUserPhotoDone += Chat.GetUserPhoto;
+ a = this;
+ }
+ Chat chat = new Chat();
+ private void Login_button_Click(object sender, RoutedEventArgs e)
+ {
+ chat.Log(AccountBox.Text,passwordBox.Password);
+ }
+
+ }
+}
diff --git a/StormChatWPF/MainWindow.xaml b/StormChatWPF/MainWindow.xaml
index 2a03a35..f6a31f3 100644
--- a/StormChatWPF/MainWindow.xaml
+++ b/StormChatWPF/MainWindow.xaml
@@ -5,17 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StormChatWPF"
mc:Ignorable="d"
- Title="MainWindow" Height="421.4" Width="628.2">
+ Title="UserWindow" Height="450" Width="800"
+
+ >
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
diff --git a/StormChatWPF/MainWindow.xaml.cs b/StormChatWPF/MainWindow.xaml.cs
index ee20688..59f1481 100644
--- a/StormChatWPF/MainWindow.xaml.cs
+++ b/StormChatWPF/MainWindow.xaml.cs
@@ -1,5 +1,4 @@
-using Interact;
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -11,50 +10,30 @@ using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
using System.Windows.Shapes;
namespace StormChatWPF
{
///
- /// MainWindow.xaml 的交互逻辑
+ /// UserWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
- StormClient.OnLoginDone += HaveLogin;
}
+ private void AddUserList()
+ {
+ if (Chat.userlist.Any())
+ {
+ foreach (var user in Chat.userlist)
+ {
+ UsersList.Items.Add(user);
+ }
+ }
+ }//向联系人列表listview中添加元素
+
- private void Login_button_Click(object sender, RoutedEventArgs e)
- {
- if (StormClient.Initialize())
- {
- StormClient.QueueLogin(AccountBox.Text, passwordBox.Password, null);
- }
- else
- {
- MessageBox.Show("连接服务器失败!");
- }
- }
-
- private void HaveLogin(ResultHead head, User user)
- {
- App.Current.Dispatcher.Invoke((Action)delegate ()
- {
- if (head.Error == "")
- {
- User.Me = user;
- UserWindow userWindow = new UserWindow();
- userWindow.Show();
- this.Close();
- }
- else
- {
- MessageBox.Show("登录失败!");
- }
- });
- }
}
}
diff --git a/StormChatWPF/StormChatWPF.csproj b/StormChatWPF/StormChatWPF.csproj
index 41589ed..de5ebf1 100644
--- a/StormChatWPF/StormChatWPF.csproj
+++ b/StormChatWPF/StormChatWPF.csproj
@@ -1,118 +1,119 @@
-
-
-
-
- Debug
- AnyCPU
- {CF4D6BE0-A862-401F-A3A6-4FDAC32F5B70}
- WinExe
- StormChatWPF
- StormChatWPF
- v4.6.1
- 512
- {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
- 4
- true
- true
-
-
- AnyCPU
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
-
-
- AnyCPU
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
-
-
- StormChatWPF.App
-
-
-
-
-
-
-
-
-
-
-
-
- 4.0
-
-
-
-
-
-
-
- MSBuild:Compile
- Designer
-
-
- UserWindow.xaml
-
-
- MSBuild:Compile
- Designer
-
-
- App.xaml
- Code
-
-
- MainWindow.xaml
- Code
-
-
- Designer
- MSBuild:Compile
-
-
-
-
- Code
-
-
- True
- True
- Resources.resx
-
-
- True
- Settings.settings
- True
-
-
- ResXFileCodeGenerator
- Resources.Designer.cs
-
-
- SettingsSingleFileGenerator
- Settings.Designer.cs
-
-
-
-
-
-
-
-
-
-
- {cef054af-95b7-4b88-934e-02705feea554}
- Interact
-
-
-
+
+
+
+
+ Debug
+ AnyCPU
+ {CF4D6BE0-A862-401F-A3A6-4FDAC32F5B70}
+ WinExe
+ StormChatWPF
+ StormChatWPF
+ v4.6.1
+ 512
+ {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
+ 4
+ true
+ true
+
+
+ AnyCPU
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ AnyCPU
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+ StormChatWPF.App
+
+
+
+
+
+
+
+
+
+
+
+
+ 4.0
+
+
+
+
+
+
+
+ MSBuild:Compile
+ Designer
+
+
+
+ MainWindow.xaml
+
+
+ MSBuild:Compile
+ Designer
+
+
+ App.xaml
+ Code
+
+
+ LogWindow.xaml
+ Code
+
+
+ Designer
+ MSBuild:Compile
+
+
+
+
+ Code
+
+
+ True
+ True
+ Resources.resx
+
+
+ True
+ Settings.settings
+ True
+
+
+ ResXFileCodeGenerator
+ Resources.Designer.cs
+
+
+ SettingsSingleFileGenerator
+ Settings.Designer.cs
+
+
+
+
+
+
+
+
+
+
+ {cef054af-95b7-4b88-934e-02705feea554}
+ Interact
+
+
+
\ No newline at end of file
diff --git a/StormChatWPF/UserWindow.xaml b/StormChatWPF/UserWindow.xaml
deleted file mode 100644
index 4f0b28f..0000000
--- a/StormChatWPF/UserWindow.xaml
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-
-
-
diff --git a/StormChatWPF/UserWindow.xaml.cs b/StormChatWPF/UserWindow.xaml.cs
deleted file mode 100644
index 990aa17..0000000
--- a/StormChatWPF/UserWindow.xaml.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Shapes;
-
-namespace StormChatWPF
-{
- ///
- /// UserWindow.xaml 的交互逻辑
- ///
- public partial class UserWindow : Window
- {
- public UserWindow()
- {
- InitializeComponent();
- }
- }
-}