public void IncomingMessage(agsXMPP.protocol.client.Message msg)
{
#region JEP-0085 Contact's Client Sends Content Message Reply With <active/> Notification
/*
<message
from='juliet@capulet.com/balcony'
to='romeo@shakespeare.lit/orchard'
type='chat'>
<thread>act2scene2chat1</thread>
<body>
What man art thou that thus bescreen'd in night
So stumblest on my counsel?
</body>
<active xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
// Once receive message from Contact Client we set ChatState Active
if (msg.Chatstate == Chatstate.active)
{
IsChatStateActive = true;
_HasReceiveChatStateActivation = HasReceiveChatStateActivation;
}
//else
// MessageBox.Show("Contact Client Chat State is not actived");
rtfChat.
SelectionColor = Color.
Red;
rtfChat.AppendText(_nickname + " said: ");
rtfChat.
SelectionColor = Color.
Black;
rtfChat.AppendText(msg.Body);
rtfChat.AppendText("\r\n");
this.chatstatemsg.Text = StatusMessage;
}
private void cmdSend_Click
(object sender,
System.
EventArgs e
)
{
agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
if (!IsChatStateActive)
{
_messageThreadID = Hash.Sha1Hash(Id.GetNextId() + _connection.MyJID.ToString() + m_Jid.ToString());
}
IsChatStateActive = true;
_HasReceiveChatStateActivation = HasReceiveChatStateActivation;
msg.Type = MessageType.chat;
msg.To = m_Jid;
msg.From = _connection.MyJID;
msg.Body = rtfSend.Text;
msg.Chatstate = Chatstate.active;
_connection.Send(msg);
OutgoingMessage(msg);
rtfSend.Text = "";
}
private void MessageCallback(object sender, agsXMPP.protocol.client.Message msg, object data)
{
if (InvokeRequired)
{
// Windows Forms are not Thread Safe, we need to invoke this :(
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new MessageCB(MessageCallback), new object[] { sender, msg, data });
return;
}
if (msg.Body != null)
{
DateTime dt = DateTime.Now;
ReceiveLastMessageDate = dt.ToString("D", DateTimeFormatInfo.InvariantInfo);
ReceiveLastMessageTime = dt.ToString("T", DateTimeFormatInfo.InvariantInfo);
this.chatstatemsg.Text = "Last message received at " + ReceiveLastMessageTime + " on " + ReceiveLastMessageDate;
IncomingMessage(msg);
}
else if (msg.Body == null && msg.Chatstate == Chatstate.composing)
{
this.chatstatemsg.Text = _nickname + " is writing a message";
}
else if (msg.Body == null && msg.Chatstate == Chatstate.gone)
{
IsChatStateActive = false;
_isChatStateGone = true;
}
else if (msg.Body == null && msg.Chatstate == Chatstate.paused)
{
timerChatStatePause.Enabled = true;
}
else if (msg.Body == null && msg.Chatstate == Chatstate.inactive)
{
HasReceiveChatStateActivation = false;
}
else if (msg.Body == null && msg.Chatstate == Chatstate.active)
{
HasReceiveChatStateActivation = true;
}
}
private void timerChatStatePause_Tick(object sender, EventArgs e)
{
this.
chatstatemsg.
Text = String.
Empty;
timerChatStatePause.Enabled = false;
}
private void rtfSend_TextChanged(object sender, EventArgs e)
{
this.
chatstatemsg.
Text = String.
Empty;
}
private void frmChat_KeyUp(object sender, KeyEventArgs e)
{
if (IsChatStateActive)
{
#region JEP-0085 Example 10. User's Client Sends Standalone <paused/> Notification
/*
<message
from='romeo@montague.net/orchard'
to='juliet@capulet.com/balcony'
type='chat'>
<thread>act2scene2chat1</thread>
<paused xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
if (HasReceiveChatStateActivation)
{
SendChatState(Chatstate.paused);
}
}
}
private void frmChat_KeyDown(object sender, KeyEventArgs e)
{
if (IsChatStateActive)
{
#region JEP-0085 Example 9. User's Client Sends Standalone <composing/> Notification
/*
<message
from='romeo@montague.net/orchard'
to='juliet@capulet.com/balcony'
type='chat'>
<thread>act2scene2chat1</thread>
<composing xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
if (HasReceiveChatStateActivation)
{
SendChatState(Chatstate.composing);
}
}
}
private void frmChat_FormClosing(object sender, FormClosingEventArgs e)
{
#region JEP-0085 Example 17. Contact's Client Sends Standalone <gone/> Notification
/*
<message
from='juliet@capulet.com/balcony'
to='romeo@shakespeare.lit/orchard'
type='chat'>
<thread>act2scene2chat1</thread>
<gone xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
if (_isChatStateGone)
{
return;
}
SendChatState(Chatstate.gone);
}
private void frmChat_Resize(object sender, EventArgs e)
{
if (IsChatStateActive)
{
if (this.WindowState == FormWindowState.Minimized)
{
#region JEP-0085 Example 14. Contact's Client Sends Standalone <inactive/> Notification
/*
<message
from='juliet@capulet.com/balcony'
to='romeo@shakespeare.lit/orchard'
type='chat'>
<thread>act2scene2chat1</thread>
<inactive xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
SendChatState(Chatstate.inactive);
_isMinimize = true;
HasReceiveChatStateActivation = false;
return;
}
if (this.WindowState == FormWindowState.Normal)
{
#region JEP-0085 Example 15. Contact's Client Sends Standalone <active/> Notification
/*
<message
from='juliet@capulet.com/balcony'
to='romeo@shakespeare.lit/orchard'
type='chat'>
<thread>act2scene2chat1</thread>
<active xmlns='http://jabber.org/protocol/chatstates'/>
</message>
*/
#endregion
if (_isMinimize)
{
SendChatState(Chatstate.active);
HasReceiveChatStateActivation = true;
_isMinimize = false;
return;
}
else
return;
}
}
}
private void SendChatState(Chatstate chatStateEnum)
{
agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();
msg.Type = MessageType.chat;
msg.To = m_Jid;
msg.From = _connection.MyJID;
msg.Chatstate = chatStateEnum;
_connection.Send(msg);
}