我正在开发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。