简体   繁体   中英

how to connect Signalr client to Signalr server on Different computers using winform

Hello guys i am making a server client architecture where each client is on different computer and server is on different computers so all client must connect to server but it's not working on different computers ...2 days ago it was working on different computers but after formatting my device now it's only working on same computer using ipaddress...

here is my code for server...

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

and for client ...

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

this is sample for this website for more information about this you can check out this ... https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b/sourcecode?fileId=119892&pathId=583880341

please help me i have been stuck on this from 1 day...thank you

In my case it was the anti-virus causing the issue. The application worked, once I disabled it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM