c# - 局域网上的 SignalR 托管
问题描述
我正在开发winform应用程序。它在两台计算机上工作。MSSQLSERVER 安装在主(主机)计算机上。该程序在主计算机上运行,没有错误。
我有一台 SignalR 服务器(Windows 窗体),它托管在 IP 为(192.168.1.5)的计算机上
但是,当我输入该 IP 地址时,程序无法运行。它总是在本地主机上工作,为什么?
public partial class WinFormsClient : Form
{
/// <summary>
/// This name is simply added to sent messages to identify the user; this
/// sample does not include authentication.
/// </summary>
private String UserName { get; set; }
private IHubProxy HubProxy { get; set; }
const string ServerURI = "http://localhost:1313/signalr";
private HubConnection Connection { get; set; }
internal WinFormsClient()
{
InitializeComponent();
}
private void ButtonSend_Click(object sender, EventArgs e)
{
HubProxy.Invoke("Send", UserName, TextBoxMessage.Text);
TextBoxMessage.Text = String.Empty;
TextBoxMessage.Focus();
}
/// <summary>
/// Creates and connects the hub connection and hub proxy. This method
/// is called asynchronously from SignInButton_Click.
/// </summary>
private async void ConnectAsync()
{
Connection = new HubConnection(ServerURI);
Connection.Closed += Connection_Closed;
HubProxy = Connection.CreateHubProxy("MyHub");
//Handle incoming event from server: use Invoke to write to console from SignalR's thread
HubProxy.On<string, string>("AddMessage", (name, message) =>
this.Invoke((Action)(() =>
RichTextBoxConsole.AppendText(String.Format("{0}: {1}" + Environment.NewLine, name, message))
))
);
HubProxy.On<string>("CallForReport", (message) =>
this.Invoke((Action)(() =>
RichTextBoxConsole.AppendText(String.Format("{0}" + Environment.NewLine, message))
))
);
try
{
await Connection.Start();
}
catch (HttpRequestException)
{
StatusText.Text = "Unable to connect to server: Start server before connecting clients.";
//No connection: Don't enable Send button or show chat UI
return;
}
//Activate UI
SignInPanel.Visible = false;
ChatPanel.Visible = true;
ButtonSend.Enabled = true;
TextBoxMessage.Focus();
RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + Environment.NewLine);
}
/// <summary>
/// If the server is stopped, the connection will time out after 30 seconds (default), and the
/// Closed event will fire.
/// </summary>
private void Connection_Closed()
{
//Deactivate chat UI; show login UI.
this.Invoke((Action)(() => ChatPanel.Visible = false));
this.Invoke((Action)(() => ButtonSend.Enabled = false));
this.Invoke((Action)(() => StatusText.Text = "You have been disconnected."));
this.Invoke((Action)(() => SignInPanel.Visible = true));
}
private void SignInButton_Click(object sender, EventArgs e)
{
UserName = UserNameTextBox.Text;
//Connect to server (use async method to avoid blocking UI thread)
if (!String.IsNullOrEmpty(UserName))
{
StatusText.Visible = true;
StatusText.Text = "Connecting to server...";
ConnectAsync();
}
}
private void WinFormsClient_FormClosing(object sender, FormClosingEventArgs e)
{
if (Connection != null)
{
Connection.Stop();
Connection.Dispose();
}
}
}
我想将 localhost 更改为 (192.168.1.5:1313) 之后,我想从客户端接收消息到服务器。
客户端 1 IP 为 (192.168.1.5) 客户端 2 IP 为 (192.168.1.20)
SignlaR Hub + MSSQLSERVER + Client1 在同一台计算机上。
解决方案
推荐阅读
- r - 如何将 ff 对象传递给集群/节点
- snowflake-cloud-data-platform - 雪花:如何读取 JSON 格式但包装在 XML 响应中的数据
- bash - 如何将列表传递给另一个函数中的函数调用
- oracle-sqldeveloper - SQL Developer 20.1 中的格式问题
- java - 如何为 ImageView 创建一个 id
- python - 在 Python TextBlob 上:更新时限制模型的大小
- ruby-on-rails - 错误:尝试安装 Rails / Nokogiri 时无法构建 gem 原生扩展
- if-statement - Google 表格将 HTML 标记添加到单元格内的数字中
- node.js - 如何在节点,js中创建浏览器警报
- azure - 使用 .pfx 文件签署 JWT