📄 privatechat.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Reflection;
using System.Configuration;
using ChatMediator;
using ChatClient;
namespace ChatClient
{
public delegate void PrivateUserLeft(string userName);
/// <summary>
/// Summary description for PrivateChat.
/// </summary>
///
public class PrivateChat : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox grpPrivateChat;
private System.Windows.Forms.Label lblErrorMessage;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.TextBox txtSendMessage;
private System.Windows.Forms.TextBox txtMessage;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#region My Variables
private string callerName = string.Empty;
private string myName = string.Empty;
private string message = string.Empty;
private IMediator im;
private EventsClass em;
private IClientImplement iciClient;
public event PrivateUserLeft UserLeft;
private string httpServerUrl = ConfigurationSettings.AppSettings["HttpServerURL"].ToString();
private string tcpServerUrl = ConfigurationSettings.AppSettings["TcpServerURL"].ToString();
#endregion
#region Public Properties
public string CallerName
{
get
{
return this.callerName;
}
set
{
this.callerName = value;
}
}
public string MyName
{
get
{
return this.myName;
}
set
{
this.myName = value;
}
}
public string Message
{
get
{
return this.message;
}
set
{
this.message = value;
}
}
#endregion
public PrivateChat()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grpPrivateChat = new System.Windows.Forms.GroupBox();
this.lblErrorMessage = new System.Windows.Forms.Label();
this.btnSend = new System.Windows.Forms.Button();
this.txtSendMessage = new System.Windows.Forms.TextBox();
this.txtMessage = new System.Windows.Forms.TextBox();
this.grpPrivateChat.SuspendLayout();
this.SuspendLayout();
//
// grpPrivateChat
//
this.grpPrivateChat.Controls.Add(this.lblErrorMessage);
this.grpPrivateChat.Controls.Add(this.btnSend);
this.grpPrivateChat.Controls.Add(this.txtSendMessage);
this.grpPrivateChat.Controls.Add(this.txtMessage);
this.grpPrivateChat.Location = new System.Drawing.Point(16, 8);
this.grpPrivateChat.Name = "grpPrivateChat";
this.grpPrivateChat.Size = new System.Drawing.Size(384, 312);
this.grpPrivateChat.TabIndex = 1;
this.grpPrivateChat.TabStop = false;
this.grpPrivateChat.Text = "groupBox1";
//
// lblErrorMessage
//
this.lblErrorMessage.ForeColor = System.Drawing.Color.Red;
this.lblErrorMessage.Location = new System.Drawing.Point(16, 248);
this.lblErrorMessage.Name = "lblErrorMessage";
this.lblErrorMessage.Size = new System.Drawing.Size(352, 48);
this.lblErrorMessage.TabIndex = 11;
//
// btnSend
//
this.btnSend.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
this.btnSend.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.btnSend.Location = new System.Drawing.Point(300, 184);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(72, 48);
this.btnSend.TabIndex = 10;
this.btnSend.Text = "Send";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// txtSendMessage
//
this.txtSendMessage.BackColor = System.Drawing.Color.MistyRose;
this.txtSendMessage.ForeColor = System.Drawing.Color.Maroon;
this.txtSendMessage.Location = new System.Drawing.Point(12, 184);
this.txtSendMessage.Multiline = true;
this.txtSendMessage.Name = "txtSendMessage";
this.txtSendMessage.Size = new System.Drawing.Size(272, 48);
this.txtSendMessage.TabIndex = 9;
this.txtSendMessage.Text = "Hi!!";
//
// txtMessage
//
this.txtMessage.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
this.txtMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtMessage.ForeColor = System.Drawing.Color.Blue;
this.txtMessage.Location = new System.Drawing.Point(12, 32);
this.txtMessage.Multiline = true;
this.txtMessage.Name = "txtMessage";
this.txtMessage.ReadOnly = true;
this.txtMessage.Size = new System.Drawing.Size(360, 136);
this.txtMessage.TabIndex = 8;
this.txtMessage.Text = "/**********WEL-COME TO PRIVATE CHAT ROOM ****************/";
//
// PrivateChat
//
this.AcceptButton = this.btnSend;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
this.ClientSize = new System.Drawing.Size(416, 333);
this.Controls.Add(this.grpPrivateChat);
this.Name = "PrivateChat";
this.Text = "PrivateChat";
this.Closing += new System.ComponentModel.CancelEventHandler(this.PrivateChat_Closing);
this.Load += new System.EventHandler(this.PrivateChat_Load);
this.grpPrivateChat.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private bool StartTcpServer()
{
bool result = false;
try
{
BinaryClientFormatterSinkProvider bcfs = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider bsfs = new BinaryServerFormatterSinkProvider();
bsfs.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary id = new Hashtable();
id = new Hashtable();
id["port"] = 0;
id["typeFilterLevel"] = TypeFilterLevel.Full;
id["name"] = System.Guid.NewGuid().ToString();
TcpChannel tcc = new TcpChannel(id, bcfs, bsfs);
ChannelServices.RegisterChannel(tcc);
this.im = (IMediator) Activator.GetObject(typeof(IMediator), this.tcpServerUrl);
result = true;
}
catch (RemotingException ex)
{
MessageBox.Show(ex.Message + " Stact trace: " + ex.StackTrace);
}
finally
{
}
return result;
}
public void em_GetMessageFromOthersHandler(IClientImplement ici)
{
this.iciClient = ici;
if (this.iciClient.ReceiverName.CompareTo(this.myName) == 0 ||
this.iciClient.UserName.CompareTo(this.myName) == 0)
{
Thread th = new Thread(new ThreadStart(this.GetMessageFromOtherClients));
th.Start();
}
}
private void GetMessageFromOtherClients()
{
this.txtMessage.AppendText(Environment.NewLine);
this.txtMessage.AppendText(this.iciClient.UserName + " says: " + iciClient.Message);
}
private void btnSend_Click(object sender, System.EventArgs e)
{
try
{
if (this.em == null)
{
this.StartTcpServer();
this.em = new EventsClass();
this.em.ReceiverName = this.callerName;
this.em.UserName = this.myName;
this.em.UserName = this.myName + "|" + this.callerName;
this.em.ReceiverName = this.callerName;
this.im.ConnectToPrivateChat(this.em);
this.em.GetMessageFromOthersHandler += new GetMessageFromOthers(this.em_GetMessageFromOthersHandler);
}
Thread th = new Thread(new ThreadStart(this.SendMessage));
th.Start();
}
catch (Exception ex)
{
MessageBox.Show("Exception occured in btnSend_Click. Error: " + ex.Message);
}
}
private void SendMessage()
{
try
{
if (this.em == null)
{
throw new ApplicationException("Client object null.");
}
this.em.Message = this.txtSendMessage.Text;
this.em.ReceiverName = this.callerName;
this.em.UserName = this.myName + "|" + this.callerName;
if (this.im == null)
{
throw new ApplicationException("Remote proxy not set.");
}
else
{
this.im.SendPrivateMessage(this.em);
this.txtSendMessage.Text = string.Empty;
}
}
catch (Exception ex)
{
MessageBox.Show("Error occured in SendMessage: Message: " + ex.Message + " Statck trace: " + ex.StackTrace);
}
}
private void PrivateChat_Load(object sender, System.EventArgs e)
{
this.grpPrivateChat.Text = this.myName + " is privately chatting with " + this.callerName;
grpPrivateChat.BackColor = Color.LightSteelBlue;
}
private void PrivateChat_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (UserLeft != null)
{
UserLeft(this.callerName);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -