【问题标题】:how to connect Signalr client to Signalr server on Different computers using winform如何使用 winform 将 Signalr 客户端连接到不同计算机上的 Signalr 服务器
【发布时间】:2016-11-24 16:33:48
【问题描述】:

大家好,我正在创建一个服务器客户端架构,其中每个客户端位于不同的计算机上,服务器位于不同的计算机上,因此所有客户端都必须连接到服务器,但它不能在不同的计算机上运行...2 天前它在不同的计算机上运行计算机,但在格式化我的设备后,它现在只能在使用 ipaddress 的同一台计算机上运行...

这是我的服务器代码...

   private IDisposable SignalR { get; set; }
   public string ServerURI = "http://" + "192.168.1.240";

    private void ButtonStart_Click(object sender, EventArgs e)
    {
        WriteToConsole("Starting server...");
        ButtonStart.Enabled = false;
        Task.Run(() => StartServer());
    }

    protected void StartServer()
    {
        try
        {
            SignalR = WebApp.Start(ServerURI);
        }
        catch (TargetInvocationException)
        {
            WriteToConsole("Server failed to start. A server is already running on " + ServerURI);
            this.Invoke((Action)(() => ButtonStart.Enabled = true));
            return;
        }
        this.Invoke((Action)(() => ButtonStop.Enabled = true));
        WriteToConsole("Server started at " + ServerURI);
    }

    internal void WriteToConsole(String message)
    {
        if (RichTextBoxConsole.InvokeRequired)
        {
            this.Invoke((Action)(() =>
                WriteToConsole(message)
            ));
            return;
        }
        RichTextBoxConsole.AppendText(message + Environment.NewLine);
    }

    private void WinFormsServer_FormClosing(object sender, FormClosingEventArgs e)
    {            
        if (SignalR != null)
        {
            SignalR.Dispose();
        }
    }
}

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

public class MyHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.addMessage(name, message);
    }
    public override Task OnConnected()
    {
        Program.MainForm.WriteToConsole("Client connected: " + Context.ConnectionId);
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        Program.MainForm.WriteToConsole("Client disconnected: " + Context.ConnectionId);
        return base.OnDisconnected(stopCalled);
    }
}

对于客户...

    private IHubProxy HubProxy { get; set; }
    string ServerURI = "http://" + "192.168.1.240" + "/signalr";
    private HubConnection Connection { get; set; }

    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))
            ))
        );
        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;
        }

        RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + Environment.NewLine);
    }

    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.Text = "Connecting to server...";
            ConnectAsync();
        }
    }

这是本网站的示例,有关更多信息,您可以查看此...https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b/sourcecode?fileId=119892&pathId=583880341

请帮助我,我从 1 天开始就一直坚持这个...谢谢

【问题讨论】:

  • 重装后防火墙是否开启?
  • thanx @MATTIAS ...是防火墙阻止了所有来电...我将其关闭并且它有效...是否有任何解决方案让我不需要关掉它...

标签: c# asp.net server client signalr


【解决方案1】:

就我而言,是防病毒软件导致了问题。该应用程序工作正常,一旦我禁用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多