xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行
Posted
技术标签:
【中文标题】xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行【英文标题】:xamarin forms signalr core client handler does not execute on ios but it executes on android 【发布时间】:2020-09-07 13:47:56 【问题描述】:我正在尝试使用 xamarin 表单和 signalR 核心构建聊天应用程序,但遇到了一个奇怪的问题 - 我能够将消息从 ios 设备发送到 android 设备,但我无法从Android 设备转 IOS 设备。
这是我的客户端信号 R 连接代码
if (hubConnection != null && hubConnection.State == HubConnectionState.Connected)
return hubConnection;
else
try
hubConnection = new HubConnectionBuilder()
//.WithUrl($"https://ip:5050/chatHub")
.WithUrl(Constants.UriHostNameType + "chatHub")
.ConfigureLogging(logging =>
logging.AddDebug();
logging.SetMinimumLevel(LogLevel.Trace);
).WithAutomaticReconnect()
.Build();
HubConnectionExtensions.On<String, String>(hubConnection, RPCName, (user, message) => OnFunction(user, message));
await Connect();
catch(Exception ex)
Debug.WriteLine(ex.Message);
return hubConnection;
这是我的客户端处理程序
try
var messageObj = JsonConvert.DeserializeObject<Message>(message);
var connection = DependencyService.Get<ISQLiteDb>().GetConnection();
connection.InsertAsync(messageObj);
messageRecievedEvent?.BeginInvoke(messageObj, null, null);
catch(Exception e)
Debug.WriteLine(e.Message);
这是我从 Android 手机发送消息时在客户端捕获的 Signalr 跟踪
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Received 21 bytes. Parsing SSE frame.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Passing message to application. Payload size: 11.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Processing 11 byte message from server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Resetting keep-alive timer, received a message from the server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Received a ping message.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Acquired the Connection Lock in order to ping the server.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Sending PingMessage message.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Debug: Sending PingMessage message completed.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Sending 11 bytes to the server using url: XXXXXXXXXXX.
Microsoft.AspNetCore.SignalR.Client.HubConnection: Trace: Releasing Connection Lock in RunTimerActions (/_/src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs:1817).
Microsoft.AspNetCore.Http.Connections.Client.Internal.LoggingHttpMessageHandler: Trace: Sending HTTP request POST 'XXXXXXXX'.
Microsoft.AspNetCore.Http.Connections.Client.Internal.ServerSentEventsTransport: Debug: Message(s) sent successfully.
这是我的服务器集线器代码
public async Task SendMessage(string userEmail, string message, string targetUserToken)
var messageObj = JsonConvert.DeserializeObject<Message>(message);
try
await Clients.Group(userEmail).SendAsync("IncomingMessage", userEmail, message);
catch (Exception e)
finally
await _notificationService.SendNotificationToUserUsingToken(targetUserToken, messageObj.Text, "New Message from " + messageObj.UIName, 5, message);
await InserIntoDb(messageObj);
public async Task InserIntoDb(Message obj)
using (var context = CoachingContext.GetRawContext(configuration))
context.messages.Add(obj);
await context.SaveChangesAsync();
public async Task LogInUser(string email)
await Groups.AddToGroupAsync(Context.ConnectionId, email);
public async Task RemoveFromGroup(string email)
await Groups.RemoveFromGroupAsync(Context.ConnectionId, email);
【问题讨论】:
【参考方案1】:您可以通过Constants.UriHostNameType
检查它是否符合App Transport Security in Xamarin.iOS。
或者,您可以对应用的 Info.plist
文件进行以下更改,以完全禁用所有域和互联网通信的 ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
【讨论】:
【参考方案2】:我有同样的问题,我想在升级到 iOS 14 之后。检查 SignalR nuget 的版本,并尝试将所有项目升级到初步版本 5.0 rc。
【讨论】:
以上是关于xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行的主要内容,如果未能解决你的问题,请参考以下文章