Last active
September 16, 2019 12:38
-
-
Save siniypin/5f403be8dc6e5b2f802e926c1b7855d3 to your computer and use it in GitHub Desktop.
Synchronizing SIP agent events with UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChatControl | |
{ | |
private void client_LoginStateChanged(object sender, EventArgs e) | |
{ | |
if (client.LoginState == LoginStateEnum.Logon) | |
client.Messanger.IncomingMessage += Messanger_IncomingMessage; | |
if ((client.LoginState == LoginStateEnum.Logoff || client.LoginState == LoginStateEnum.LoginError) | |
&& client.Messanger != null) | |
client.Messanger.IncomingMessage -= Messanger_IncomingMessage; | |
} | |
private void Messanger_IncomingMessage(object sender, PagerEventArgs e) | |
{ | |
ChatMessInfo mess = e.Body; | |
List<ChatMessInfo> chatMessages = null; | |
if (!conversations.TryGetValue(contact, out chatMessages)) | |
{ | |
chatMessages = new List<ChatMessInfo>(); | |
conversations.Add(contact, chatMessages); | |
} | |
chatMessages.Add(mess); | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InstantMessengerService : IInstantMessangerService | |
{ | |
private SynchronizationContext _syncCtx; | |
public event EventHandler<PagerEventArgs> IncomingMessage = delegate { }; | |
public InstantMessangerService(Account chatAccount) | |
{ | |
_sipUserAgent.IncomingMessage += OnIncomingMessage; | |
_syncCtx = SynchronizationContext.Current; | |
} | |
private void OnIncomingMessage(object sender, PagerEventArgs e) | |
{ | |
try | |
{ | |
_syncCtx.Post(delegate | |
{ | |
// read and parse msg | |
ChatMessInfo chatInfo = new ChatMessInfo(StringExtensions.ExtractUid(e.From), docx.DocumentElement); | |
IncomingMessage(this, new PagerEventArgs(e, chatInfo)); | |
}, null); | |
} | |
catch (InvalidOperationException) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment