338 lines
14 KiB
C#
338 lines
14 KiB
C#
|
using DevExpress.XtraEditors;
|
|||
|
using System;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Windows.Forms;
|
|||
|
using HL_FristAidPlatform_Public;
|
|||
|
|
|||
|
|
|||
|
namespace HL_FristAidPlatform_MultiSystemPublic
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 专家会诊
|
|||
|
/// </summary>
|
|||
|
public partial class Form_Consultation : XtraForm
|
|||
|
{
|
|||
|
#region 变量
|
|||
|
Socket clientSocket = null;
|
|||
|
static bool isListen = true;
|
|||
|
Thread thDataFromServer;
|
|||
|
/// <summary>
|
|||
|
/// 服务器IP地址
|
|||
|
/// </summary>
|
|||
|
IPAddress ipAddress;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 专家会诊服务端部署IP
|
|||
|
/// </summary>
|
|||
|
private string Config101 = PublicClassForDataBase.Config101;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 当前用户名
|
|||
|
/// </summary>
|
|||
|
private string Cur_UserName = string.Empty;
|
|||
|
#endregion
|
|||
|
|
|||
|
public Form_Consultation()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
ipAddress = IPAddress.Loopback;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 窗体加载
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void Form_Consultation_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
txt_ServerIP.Text = Config101;
|
|||
|
txt_UserName.Text = Cur_UserName = Information.User.FullName;
|
|||
|
//连接服务
|
|||
|
Connect(Config101);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发生消息
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void btn_Send_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
SendMessage();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发送消息
|
|||
|
/// </summary>
|
|||
|
private void SendMessage()
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(txt_EditMessages.Text.Trim()))
|
|||
|
{
|
|||
|
XtraMessageBox.Show("不能发生空白信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
byte[] bytesSend = Encoding.UTF8.GetBytes(txt_EditMessages.Text + "$");
|
|||
|
clientSocket.Send(bytesSend);
|
|||
|
txt_EditMessages.Text = "";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
XtraMessageBox.Show("未连接服务器或者服务器已停止,请联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 连接服务器
|
|||
|
/// </summary>
|
|||
|
/// <param name="connectIp">要连接的服务器IP</param>
|
|||
|
private void Connect(string connectIp)
|
|||
|
{
|
|||
|
if (Cur_UserName.Length >= 17 && Cur_UserName.ToString().Trim().Substring(0, 17).Equals("Server has closed"))
|
|||
|
{
|
|||
|
XtraMessageBox.Show("该用户名中包含敏感词,请更换用户名后重试", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (clientSocket == null || !clientSocket.Connected)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
//参考网址: https://msdn.microsoft.com/zh-cn/library/6aeby4wt.aspx
|
|||
|
// Socket.BeginConnect 方法 (String, Int32, AsyncCallback, Object)
|
|||
|
//开始一个对远程主机连接的异步请求
|
|||
|
/* string host, 远程主机名
|
|||
|
* int port, 远程主机的端口
|
|||
|
* AsyncCallback requestCallback, 一个 AsyncCallback 委托,它引用连接操作完成时要调用的方法,也是一个异步的操作
|
|||
|
* object state 一个用户定义对象,其中包含连接操作的相关信息。 当操作完成时,此对象会被传递给 requestCallback 委托
|
|||
|
*/
|
|||
|
//如果txt_ServerIP里面有值,就选择填入的IP作为服务器IP,不填的话就默认是本机的
|
|||
|
if (!string.IsNullOrEmpty(connectIp))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
ipAddress = IPAddress.Parse(connectIp);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
XtraMessageBox.Show("请在配置参数表正确配置参数101的属性!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ipAddress = IPAddress.Loopback;
|
|||
|
}
|
|||
|
//IPAddress ipadr = IPAddress.Parse("192.168.1.100");
|
|||
|
clientSocket.BeginConnect(ipAddress, 8080, (args) =>
|
|||
|
{
|
|||
|
if (args.IsCompleted) //判断该异步操作是否执行完毕
|
|||
|
{
|
|||
|
byte[] bytesSend = new byte[4096];
|
|||
|
try
|
|||
|
{
|
|||
|
txt_UserName.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
bytesSend = Encoding.UTF8.GetBytes(txt_UserName.Text.Trim() + "$"); //用户名,这里是刚刚连接上时需要传过去
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
clientSocket.Send(bytesSend);
|
|||
|
txt_UserName.Enabled = false; //设置为不能再改名字了
|
|||
|
txt_EditMessages.Focus(); //将焦点放在
|
|||
|
thDataFromServer = new Thread(DataFromServer);
|
|||
|
thDataFromServer.IsBackground = true;
|
|||
|
thDataFromServer.Start();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
XtraMessageBox.Show("服务器已关闭", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
txt_ServerIP.ReadOnly = false;
|
|||
|
}
|
|||
|
}));
|
|||
|
txt_ServerIP.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
txt_ServerIP.Enabled = false;
|
|||
|
}
|
|||
|
}));
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
}
|
|||
|
}
|
|||
|
}, null);
|
|||
|
}
|
|||
|
catch (SocketException ex)
|
|||
|
{
|
|||
|
XtraMessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取服务器端的消息
|
|||
|
/// </summary>
|
|||
|
private void DataFromServer()
|
|||
|
{
|
|||
|
ShowMessages("连接到会诊服务器...");
|
|||
|
isListen = true;
|
|||
|
try
|
|||
|
{
|
|||
|
while (isListen)
|
|||
|
{
|
|||
|
byte[] bytesFrom = new byte[4096];
|
|||
|
int len = clientSocket.Receive(bytesFrom);
|
|||
|
|
|||
|
string dataFromClient = Encoding.UTF8.GetString(bytesFrom, 0, len);
|
|||
|
if (!string.IsNullOrWhiteSpace(dataFromClient))
|
|||
|
{
|
|||
|
//如果收到服务器已经关闭的消息,那么就把客户端接口关了,免得出错,并在客户端界面上显示出来
|
|||
|
if (dataFromClient.ToString().Length >= 17 && dataFromClient.ToString().Substring(0, 17).Equals("Server has closed"))
|
|||
|
{
|
|||
|
clientSocket.Close();
|
|||
|
clientSocket = null;
|
|||
|
|
|||
|
txt_ReceiveMessages.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_ReceiveMessages.Text += Environment.NewLine + "服务器已关闭";
|
|||
|
txt_ReceiveMessages.SelectionStart = txt_ReceiveMessages.Text.Length;
|
|||
|
txt_ReceiveMessages.ScrollToCaret();
|
|||
|
}));
|
|||
|
|
|||
|
txt_UserName.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_UserName.Enabled = true;
|
|||
|
}));
|
|||
|
//重连当然可以换用户名啦
|
|||
|
|
|||
|
txt_ServerIP.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_ServerIP.Enabled = true;
|
|||
|
}));
|
|||
|
|
|||
|
thDataFromServer.Abort();//这一句必须放在最后,不然这个进程都关了后面的就不会执行了
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (dataFromClient.StartsWith("#") && dataFromClient.EndsWith("#"))
|
|||
|
{
|
|||
|
string userName = dataFromClient.Substring(1, dataFromClient.Length - 2);
|
|||
|
BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
MessageBox.Show("用户名:[" + userName + "]已经存在,请尝试其他用户名并重试");
|
|||
|
|
|||
|
}));
|
|||
|
isListen = false;
|
|||
|
|
|||
|
txt_UserName.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_UserName.Enabled = true;
|
|||
|
clientSocket.Send(Encoding.UTF8.GetBytes("$"));
|
|||
|
clientSocket.Close();
|
|||
|
clientSocket = null;
|
|||
|
}));
|
|||
|
txt_ServerIP.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_ServerIP.Enabled = true;
|
|||
|
}));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//txt_UserName.Enabled = false; //当用户名唯一时才禁止再次输入用户名
|
|||
|
ShowMessages(dataFromClient);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (SocketException ex)
|
|||
|
{
|
|||
|
isListen = false;
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
//没有在客户端关闭连接,而是给服务器发送一个消息,在服务器端关闭连接
|
|||
|
//这样可以将异常的处理放到服务器。客户端关闭会让客户端和服务器都抛异常
|
|||
|
clientSocket.Send(Encoding.UTF8.GetBytes("$"));
|
|||
|
MessageBox.Show(ex.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 显示消息
|
|||
|
/// </summary>
|
|||
|
/// <param name="messages"></param>
|
|||
|
private void ShowMessages(string messages)
|
|||
|
{
|
|||
|
txt_ReceiveMessages.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_ReceiveMessages.Text += Environment.NewLine + messages; // 在 Windows 环境中,C# 语言 Environment.NewLine == "\r\n" 结果为 true
|
|||
|
txt_ReceiveMessages.SelectionStart = txt_ReceiveMessages.Text.Length;
|
|||
|
txt_ReceiveMessages.ScrollToCaret();
|
|||
|
}));
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 窗体激活时鼠标焦点定位到消息框
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void Form_Consultation_Activated(object sender, EventArgs e)
|
|||
|
{
|
|||
|
txt_EditMessages.Focus();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 聊天窗口关闭后
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void Form_Consultation_FormClosed(object sender, FormClosedEventArgs e)
|
|||
|
{
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
clientSocket.Send(Encoding.UTF8.GetBytes("$"));
|
|||
|
ExitConnect();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 退出聊天
|
|||
|
/// </summary>
|
|||
|
private void ExitConnect()
|
|||
|
{
|
|||
|
if (clientSocket != null && clientSocket.Connected)
|
|||
|
{
|
|||
|
thDataFromServer.Abort();
|
|||
|
clientSocket.Send(Encoding.UTF8.GetBytes("$"));
|
|||
|
|
|||
|
clientSocket.Close();
|
|||
|
clientSocket = null;
|
|||
|
|
|||
|
txt_ReceiveMessages.BeginInvoke(new Action(() =>
|
|||
|
{
|
|||
|
txt_ReceiveMessages.Text += Environment.NewLine + "已断开与服务器的连接";
|
|||
|
txt_ReceiveMessages.SelectionStart = txt_ReceiveMessages.Text.Length;
|
|||
|
txt_ReceiveMessages.ScrollToCaret();
|
|||
|
}));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 重新连接服务器
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void btn_RefConnect_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
Connect(txt_ServerIP.Text.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|