diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs index 451546b..8658a03 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChannelTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -199,4 +199,4 @@ public async Task TestEmitUserMention() var received = receivedManualEvent.WaitOne(7000); Assert.True(received); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatEventTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatEventTests.cs index 6931d90..4f2dccb 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatEventTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatEventTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -55,4 +55,4 @@ public async Task TestModerationEvents() var moderationEventReceived = manualModerationEvent.WaitOne(5000); Assert.IsTrue(moderationEventReceived); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs index 48a9235..25be6ec 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ChatTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; using PubnubChatApi.Enums; @@ -227,4 +227,4 @@ public async Task TestCanI() Assert.True(await accessChat.ChatAccessManager.CanI(PubnubAccessPermission.Read, PubnubAccessResourceType.Channels, "can_i_test_channel")); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MembershipTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MembershipTests.cs index 7e93830..ab9bee7 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MembershipTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MembershipTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -147,4 +147,4 @@ public async Task TestUnreadMessagesCount() .FirstOrDefault(x => x.ChannelId == unreadChannel.Id); Assert.True(membership != null && await membership.GetUnreadMessagesCount() == 3); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageDraftTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageDraftTests.cs index 6787cdf..98038e3 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageDraftTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageDraftTests.cs @@ -1,4 +1,4 @@ -/*using PubNubChatAPI.Entities; +using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; namespace PubNubChatApi.Tests; @@ -166,4 +166,4 @@ public async Task TestSend() var gotCallback = successReset.WaitOne(6000); Assert.True(gotCallback); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageTests.cs index 7b678d5..8a1a40e 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/MessageTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -250,9 +250,9 @@ public async Task TestCreateThread() try { var thread = await message.CreateThread(); - await Task.Delay(3000); + await Task.Delay(2000); await thread.SendText("thread_init_text"); - await Task.Delay(3000); + await Task.Delay(2000); hasThread = message.HasThread(); } catch (Exception e) @@ -265,7 +265,7 @@ public async Task TestCreateThread() Assert.True(message.TryGetThread(out var threadChannel)); await message.RemoveThread(); - await Task.Delay(8000); + await Task.Delay(5000); //TODO: temporary way to get latest message pointer since remove_thread doesn't return a new pointer chat.TryGetMessage(channel.Id, message.Id, out message); @@ -275,7 +275,7 @@ public async Task TestCreateThread() }; await channel.SendText("thread_start_message"); - var received = manualReceiveEvent.WaitOne(20000); + var received = manualReceiveEvent.WaitOne(25000); Assert.IsTrue(received); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/RestrictionsTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/RestrictionsTests.cs index 4cb6d35..a688520 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/RestrictionsTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/RestrictionsTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -79,4 +79,4 @@ public async Task TestGetRestrictionsSets() Assert.True(a.Restrictions.Any(x => x.UserId == user.Id)); Assert.True(b.Restrictions.Any(x => x.ChannelId == channel.Id)); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ThreadsTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ThreadsTests.cs index 95af9f9..3485256 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ThreadsTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/ThreadsTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -184,4 +184,4 @@ public async Task TestThreadMessageUpdate() var updated = messageUpdatedReset.WaitOne(255000); Assert.True(updated); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs index 8a5c33d..4f8b9fd 100644 --- a/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs +++ b/c-sharp-chat/PubnubChatApi/PubNubChatApi.Tests/UserTests.cs @@ -1,4 +1,4 @@ -/*using System.Diagnostics; +using System.Diagnostics; using PubNubChatAPI.Entities; using PubnubChatApi.Entities.Data; @@ -76,4 +76,4 @@ await testUser.Update(new ChatUserData() var updated = updatedReset.WaitOne(8000); Assert.True(updated); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Channel.cs b/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Channel.cs index 03f3762..b4f2f76 100644 --- a/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Channel.cs +++ b/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Channel.cs @@ -557,6 +557,10 @@ public MessageDraft CreateMessageDraft(UserSuggestionSource userSuggestionSource /// public async Task Connect() { + if (connected) + { + return; + } connected = true; var buffer = new StringBuilder(4096); CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_connect(pointer, buffer))); @@ -588,6 +592,10 @@ public async Task Connect() /// public async Task Join() { + if (connected) + { + return; + } connected = true; var buffer = new StringBuilder(4096); CUtilities.CheckCFunctionResult(await Task.Run(() => pn_channel_join(pointer, string.Empty, buffer))); diff --git a/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Chat.cs b/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Chat.cs index e1f78e0..fbcb20e 100644 --- a/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Chat.cs +++ b/c-sharp-chat/PubnubChatApi/PubnubChatApi/Entities/Chat.cs @@ -306,8 +306,6 @@ private static extern int pn_chat_get_current_user_mentions(IntPtr chat, string private Dictionary membershipWrappers = new(); private Dictionary messageWrappers = new(); private bool fetchUpdates = true; - private Thread fetchUpdatesThread; - private SynchronizationContext? context; public event Action OnReportEvent; public event Action OnModerationEvent; @@ -328,18 +326,15 @@ private static extern int pn_chat_get_current_user_mentions(IntPtr chat, string /// /// /// Config with PubNub keys and values - /// Synchronization context on which the chat callbacks should be called, - /// defaults to SynchronizationContext.Current assigned in the Chat constructor. /// /// The constructor initializes the chat instance with the provided keys and user ID from the Config. /// - public static async Task CreateInstance(PubnubChatConfig config, - SynchronizationContext? synchronizationContext = null) + public static async Task CreateInstance(PubnubChatConfig config) { - return await Task.Run(() => new Chat(config, synchronizationContext)); + return await Task.Run(() => new Chat(config)); } - internal Chat(PubnubChatConfig config, SynchronizationContext? synchronizationContext = null) + internal Chat(PubnubChatConfig config) { chatPointer = pn_chat_new(config.PublishKey, config.SubscribeKey, config.UserId, config.AuthKey, config.TypingTimeout, config.TypingTimeoutDifference, config.StoreUserActivityInterval, @@ -348,27 +343,25 @@ internal Chat(PubnubChatConfig config, SynchronizationContext? synchronizationCo Config = config; ChatAccessManager = new ChatAccessManager(chatPointer); - context = synchronizationContext ?? SynchronizationContext.Current; - fetchUpdatesThread = new Thread(FetchUpdatesLoop) { IsBackground = true }; - fetchUpdatesThread.Start(); + FetchUpdatesLoop(); } #region Updates handling - private async void FetchUpdatesLoop() + private async Task FetchUpdatesLoop() { while (fetchUpdates) { var updates = await GetUpdates(); ParseJsonUpdatePointers(updates); - await Task.Delay(500); + await Task.Delay(200); } } internal void ParseJsonUpdatePointers(string jsonPointers) { - void Post(SendOrPostCallback callback, object? state) + /*void Post(SendOrPostCallback callback, object? state) { if (context != null) { @@ -378,7 +371,7 @@ void Post(SendOrPostCallback callback, object? state) { callback.Invoke(state); } - } + }*/ if (!string.IsNullOrEmpty(jsonPointers) && jsonPointers != "[]") { @@ -414,7 +407,10 @@ void Post(SendOrPostCallback callback, object? state) if (TryGetChannel(chatEvent.ChannelId, out var typingChannel) && typingChannel.TryParseAndBroadcastTypingEvent(chatEvent)) { - Post(delegate { OnTypingEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnTypingEvent?.Invoke(chatEvent); + //}, null); } else { @@ -423,27 +419,48 @@ void Post(SendOrPostCallback callback, object? state) break; case PubnubChatEventType.Report: - Post(delegate { OnReportEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnReportEvent?.Invoke(chatEvent); + //}, null); break; case PubnubChatEventType.Receipt: - Post(delegate { OnReadReceiptEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnReadReceiptEvent?.Invoke(chatEvent); + //}, null); if (TryGetChannel(chatEvent.ChannelId, out var readReceiptChannel)) { - Post(delegate { readReceiptChannel.BroadcastReadReceipt(chatEvent); }, null); + //Post(delegate + //{ + readReceiptChannel.BroadcastReadReceipt(chatEvent); + //}, null); } break; case PubnubChatEventType.Mention: - Post(delegate { OnMentionEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnMentionEvent?.Invoke(chatEvent); + //}, null); break; case PubnubChatEventType.Invite: - Post(delegate { OnInviteEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnInviteEvent?.Invoke(chatEvent); + //}, null); break; case PubnubChatEventType.Custom: - Post(delegate { OnCustomEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnCustomEvent?.Invoke(chatEvent); + //}, null); break; case PubnubChatEventType.Moderation: - Post(delegate { OnModerationEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnModerationEvent?.Invoke(chatEvent); + //}, null); break; default: throw new ArgumentOutOfRangeException(); @@ -451,7 +468,10 @@ void Post(SendOrPostCallback callback, object? state) if (!failedToInvoke) { - Post(delegate { OnAnyEvent?.Invoke(chatEvent); }, null); + //Post(delegate + //{ + OnAnyEvent?.Invoke(chatEvent); + //}, null); } pn_dispose_message(pointer); @@ -470,7 +490,10 @@ void Post(SendOrPostCallback callback, object? state) var timeToken = Message.GetMessageIdFromPtr(threadMessagePointer); var message = new ThreadMessage(this, threadMessagePointer, timeToken); messageWrappers[timeToken] = message; - Post(delegate { threadChannel.BroadcastMessageReceived(message); }, null); + //Post(delegate + //{ + threadChannel.BroadcastMessageReceived(message); + //}, null); } pn_dispose_message(pointer); @@ -489,7 +512,10 @@ void Post(SendOrPostCallback callback, object? state) var timeToken = Message.GetMessageIdFromPtr(messagePointer); var message = new Message(this, messagePointer, timeToken); messageWrappers[timeToken] = message; - Post(delegate { channel.BroadcastMessageReceived(message); }, null); + //Post(delegate + //{ + channel.BroadcastMessageReceived(message); + //}, null); } pn_dispose_message(pointer); continue; @@ -506,7 +532,10 @@ void Post(SendOrPostCallback callback, object? state) if (existingMessageWrapper is ThreadMessage existingThreadMessageWrapper) { existingThreadMessageWrapper.UpdateWithPartialPtr(updatedThreadMessagePointer); - Post(delegate { existingThreadMessageWrapper.BroadcastMessageUpdate(); }, null); + //Post(delegate + //{ + existingThreadMessageWrapper.BroadcastMessageUpdate(); + //}, null); } else { @@ -528,7 +557,10 @@ void Post(SendOrPostCallback callback, object? state) if (messageWrappers.TryGetValue(id, out var existingMessageWrapper)) { existingMessageWrapper.UpdateWithPartialPtr(updatedMessagePointer); - Post(delegate { existingMessageWrapper.BroadcastMessageUpdate(); }, null); + //Post(delegate + //{ + existingMessageWrapper.BroadcastMessageUpdate(); + //}, null); } pn_dispose_message(pointer); @@ -549,12 +581,18 @@ void Post(SendOrPostCallback callback, object? state) //This has a check for "PUBNUB_INTERNAL_THREAD" and will correctly update the pointer TryGetChannel(id, out var existingThreadChannel); //TODO: broadcast thread channel update (very low priority because I don't think they have that in JS chat) - Post(delegate { existingThreadChannel.BroadcastChannelUpdate(); }, null); + //Post(delegate + //{ + existingThreadChannel.BroadcastChannelUpdate(); + //}, null); } else if (channelWrappers.TryGetValue(id, out var existingChannelWrapper)) { existingChannelWrapper.UpdateWithPartialPtr(channelPointer); - Post(delegate { existingChannelWrapper.BroadcastChannelUpdate(); }, null); + //Post(delegate + //{ + existingChannelWrapper.BroadcastChannelUpdate(); + //}, null); } pn_dispose_message(pointer); @@ -571,7 +609,10 @@ void Post(SendOrPostCallback callback, object? state) if (userWrappers.TryGetValue(id, out var existingUserWrapper)) { existingUserWrapper.UpdateWithPartialPtr(userPointer); - Post(delegate { existingUserWrapper.BroadcastUserUpdate(); }, null); + //Post(delegate + //{ + existingUserWrapper.BroadcastUserUpdate(); + //}, null); } pn_dispose_message(pointer); @@ -588,7 +629,10 @@ void Post(SendOrPostCallback callback, object? state) if (membershipWrappers.TryGetValue(id, out var existingMembershipWrapper)) { existingMembershipWrapper.UpdateWithPartialPtr(membershipPointer); - Post(delegate { existingMembershipWrapper.BroadcastMembershipUpdate(); }, null); + //Post(delegate + //{ + existingMembershipWrapper.BroadcastMembershipUpdate(); + //}, null); } pn_dispose_message(pointer); @@ -610,7 +654,10 @@ void Post(SendOrPostCallback callback, object? state) if (TryGetChannel(channelId, out var channel)) { - Post(delegate { channel.BroadcastPresenceUpdate(); }, null); + //Post(delegate + //{ + channel.BroadcastPresenceUpdate(); + //}, null); } pn_dispose_message(pointer); @@ -1901,7 +1948,6 @@ public void Destroy() return; } fetchUpdates = false; - fetchUpdatesThread.Join(); pn_chat_delete(chatPointer); }