SignalR客户端与服务器SSL加密通信及跨机连接故障排查
SignalR客户端与服务器连接问题排查
我用C# .NET Framework实现了SignalR客户端与服务器的基础连接,代码如下:
客户端代码
using Microsoft.AspNet.SignalR.Client; namespace WinFormsClient { public partial class FrmClient : Form { //Connection to a SignalR server HubConnection _signalRConnection; //Proxy object for a hub hosted on the SignalR server IHubProxy _hubProxy; private async Task connectAsync() { //Set Certificate callback ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; //Create a connection for the SignalR server _signalRConnection = new HubConnection("https://10.224.10.10:12316/signalr/SimpleHub/"); _signalRConnection.StateChanged += HubConnection_StateChanged; _signalRConnection.Closed += HubConnection_Closed; //Get a proxy object that will be used to interact with the specific hub on the server //Ther may be many hubs hosted on the server, so provide the type name for the hub _hubProxy = _signalRConnection.CreateHubProxy("SimpleHub"); //Reigster to the "AddMessage" callback method of the hub //This method is invoked by the hub _hubProxy.On<string, string>("AddMessage", (name, message) => writeToLog($"{name}:{message}")); try { //Connect to the server await _signalRConnection.Start(); //Send user name for this client, so we won't need to send it with every message await _hubProxy.Invoke("SetUserName", txtUserName.Text); } catch (Exception ex) { writeToLog($"Error:{ex.Message}"); } } private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErros) { return true; } } }
服务器代码
using Microsoft.Owin.Hosting; using Microsoft.AspNet.SignalR; using System.ComponentModel; namespace WinFormsServer { public partial class FrmServer : Form { private IDisposable _signalR; private void btnStartServer_Click(object sender, EventArgs e) { try { //Start SignalR server with the give URL address //Final server address will be "URL/signalr" //Startup.Configuration is called automatically _signalR = WebApp.Start<Startup>("https://localhost:12316"); writeToLog($"Server started at:{txtUrl.Text}"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } using Owin; using Microsoft.Owin.Cors; namespace WinFormsServer { class Startup { public void Configuration(IAppBuilder app) { //CORS need to be enabled for calling SignalR service app.UseCors(CorsOptions.AllowAll); //Find and reigster SignalR hubs app.MapSignalR(); } } }
初始连接问题
客户端与服务器部署在同一机器,已通过命令Netsh http add sslcert ipport=10.224.10.10:12316 certificate=xxxx appid={xxxx}绑定自签名SSL证书到指定IP和端口。服务器启动正常,但客户端连接时报错:
StatusCode:400, ReasonPhrase:'Bad Request', Version: 1.1, Content: System.Net.Http.SteamContent, Headers: { Connection: close Data: Mon, 20 Nov 2023 04:41:15 GMT Server: Microsoft-HTTPAPI/2.0 Content-Length:334 Content-Type:text/html; charset=us-ascii }
已将自签名证书导入MMC控制台的个人和受信任根证书颁发机构(本地计算机和当前用户),问题仍存在。
另外,将服务器启动URL改为https://10.224.10.10:12316/时,服务器启动失败,提示Inner Exception HttpListenerException: Access is denied,已在本地入站规则中添加服务名称并绑定12316端口,询问是否还需其他配置。
2023-11-21 更新问题
已解决服务器启动权限问题:以管理员身份运行服务器即可使用公网IP启动SignalR服务器。但新问题出现:已配置入站规则,其他客户端机器仍无法访问SignalR服务器,客户端使用https://公网IP:12316/signalr/SimpleHub/连接时,报错:
An error occurred while sending the request. SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.224.10.10:12316
请求协助排查上述问题。
内容的提问来源于stack exchange,提问作者RunXin Shirley
相关产品推荐
相关产品推荐

