首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用winform将Signalr客户端连接到不同计算机上的Signalr服务器

如何使用winform将Signalr客户端连接到不同计算机上的Signalr服务器
EN

Stack Overflow用户
提问于 2016-07-21 15:52:12
回答 0查看 1.6K关注 0票数 3

大家好,我正在做一个服务器客户端架构,每个客户端在不同的计算机上,服务器在不同的计算机上,所以所有的客户端都必须连接到服务器,但它不能在不同的计算机上工作,几天前它在不同的计算机上工作,但在格式化我的设备后,现在它只能在使用it地址的同一台计算机上工作……...2

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

代码语言:javascript
复制
   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);
    }
}

对于客户..。

代码语言:javascript
复制
    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 day...thank你

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38498200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档