c# - xamarin 表单信号器核心客户端处理程序不在 ios 上执行,但在 android 上执行
问题描述
我正在尝试使用 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);
}
解决方案
您可以检查Constants.UriHostNameType
它是否符合 Xamarin.iOS 中的应用程序传输安全性。
或者,您可以对应用程序的Info.plist
文件进行以下更改,以完全禁用所有域和互联网通信的 ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
推荐阅读
- android - 无法找到具有路径的平台 SDK:platforms;android-31
- tailwind-css - 使用 Tailwind CSS 开发 Web UI 的基础方法
- angular - ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。URL 段:“用户”
- object - 计算图像中一个物体的x,y,height和width物体坐标来训练YOLOv2
- c - 如何使用 MPI_Scatterv 分散 3d 数组?
- groovy - Groovy 中读取响应正文中的 UnicodeToChar 转换和正则表达式
- android - 带有 Material Design 主题的 Android 应用在 Android 5 和 6 上崩溃
- solidity - DeclarationError: 未声明的标识符。您的意思是“balanceOf”还是“blances”?
- java - 使用pagedlistholder分页时,为什么current和PageSize发生变化时分页内容完全没有变化
- gmail - 有没有办法将自动回复电子邮件包含在 gmail 的方括号中?