首页 文章

SignalR更改端口 - SelfHost

提问于
浏览
1

我正在尝试使用远程请求更改服务器的端口 . 当我这样做似乎一切都好,但当我尝试连接时,我得到一个例外 .

如果我关闭(终止进程)然后打开(启动进程)具有新端口的服务器一切正常(客户端设法连接) .

Server:

internal class Program
{
    private static IDisposable _webApp = null;
    static void Main(string[] args)
    {
        var uri = Start(8089);
        Stop();
        var uri = Start(8089);
        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
    }

    private static string Start(int port)
    {
        var options = new StartOptions();
        var uri = string.Format("http://*:{0}/", port);
        options.Urls.Add(uri);
        options.Settings.Add(typeof(ITraceOutputFactory).FullName, typeof(NullTraceOutputFactory).AssemblyQualifiedName);// disable built-in owin tracing by using a null traceoutput
        var startup = new Startup
            {
                Restart = Restart
            };
        _webApp = WebApp.Start(options, startup.Configuration);
        Trace.Listeners.Remove("HostingTraceListener"); // remove the built-in owin listener
        Console.WriteLine("Server running at " + uri);
        return uri;
    }

    private static void Stop()
    {
        _webApp.Dispose();
    }

    private static string Restart(int port)
    {
        Stop();
        var uri = Start(port);
        return uri;
    }

    internal class NullTraceOutputFactory : ITraceOutputFactory
    {
        public TextWriter Create(string outputFile)
        {
            return StreamWriter.Null;
        }
    }
}

[HubName("Configuration")]
public class ConfigurationHub : Hub
{
    private Func<int, string> _restart;

    public ConfigurationHub(Func<int, string> restart)
    {
        _restart = restart;
    }

    public void ChangePort(int port)
    {
        Console.WriteLine("Port: " + port);
        _restart(port);
    }
}

internal class Startup
{
    public Func<int, string> Restart;

    public void Configuration(IAppBuilder app)
    {
        GlobalHost.DependencyResolver.Register(typeof(ConfigurationHub),
                () => new ConfigurationHub(Restart));
        app.UseCors(CorsOptions.AllowAll);
        //app.UseWelcomePage();
        var hubConfiguration = new HubConfiguration
        {
            EnableJSONP = true,
#if DEBUG
            EnableDetailedErrors = true
#endif
        };

        app.MapSignalR(hubConfiguration);
    }
}

Client:

public partial class Form1 : Form
{
    private IHubProxy _configurationProxy = null;
    private HubConnection _hubConnection;
    private const string CATEGORY = "ABCD";

    public Form1()
    {
        InitializeComponent();
        CreateProxy(8089);
        _hubConnection.Start().Wait();
    }

    private void btnChangePort_Click(object sender, EventArgs e)
    {
        var newPort = Int32.Parse(btnChangePort.Tag as string);
        _configurationProxy.Invoke("ChangePort", newPort);
        _hubConnection.Stop();
        _hubConnection.Dispose();
        _hubConnection = null;
        CreateProxy(newPort);
        _hubConnection.Start().Wait(); // <= Exception when clicking the button
    }

    private void CreateProxy(int newPort)
    {
        //var address = "127.0.0.1";
        var address = "192.168.0.100";
        var port = newPort;
        var config =
            new
                {
                    Name = "Configuration",
                    Pattern = "http://{0}:{1}",
                    Address = address,
                    Port = newPort,
                    QueryString = new Dictionary<string, string> { { "version", "1.0" } },
                    Methods = new[]
                        {
                            "notifyWrongVersion",
#if ERROR
                                "notifyConnected",
#endif
                            //"status",
                            "notifyByeBye"
                        },
                    MethodsCallbacks = new Action[]
                        {
                            () => Console.WriteLine("{0}: You are using the wrong version!", CATEGORY),
#if ERROR
                                    () => Trace.WriteLine("Connected to server! HOORAY!", Categories.CATEGORY_THIS),
#endif
                            () =>
                            Console.WriteLine("{0}: Bye bye from the server. Thank you for discnnecting.", CATEGORY)
                        },
                };

        var url = String.Format(config.Pattern, config.Address, config.Port);

        _hubConnection = new HubConnection(url);
        _hubConnection.TraceLevel = TraceLevels.All;
        _hubConnection.TraceWriter = Console.Out;
        _configurationProxy = _hubConnection.CreateHubProxy(config.Name);
    }
}

Exception:
Exception

Scenario:
在客户端和服务器上启动端口8089
按下客户端和服务器上的按钮更改为8088
关闭客户端
使用端口8088打开客户端 .

Client Logging:

14:04:18.2450308 - null - ChangeState(Disconnected,Connecting)14:04:18.7762877 - 708038b9-66b0-4cc2-95ac-de45411e453c - WS连接到:ws://192.168.250.9:8088 / signalr / connect?clientProtocol = 1.4&运输=的WebSockets&connectionData = [{ “名称”: “配置”}]&connectionToken = AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2BY%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D 14:04:23.8220044 - 708038b9-66b0- 4cc2-95ac-de45411e453c - 自动:无法连接到使用传输webSockets . System.TimeoutException:传输超时尝试连接14:04:23.8220044 - 708038b9-66b0-4cc2-95ac-de45411e453c - SSE:GET http://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&transport=serverSentEvents&connectionData= [{ “名称”: “配置”}]&connectionToken = AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2BY%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D 14:04:23.9157549 - 708038b9-66b0-4cc2-95ac-de45411e453c - WS:OnClose()14:04:23.9157549 - 708038b9-66b0-4cc2-95ac-de45411e453c - SSE:OnMessage(数据:已初始化)14:04:28.8486354 - 708038b9-66b0-4cc2-95ac-de45411e453c - 自动:连接失败使用传输serverSentEvents . System.TimeoutException:传输超时尝试连接14:04:28.8642594 - 708038b9-66b0-4cc2-95ac-de45411e453c - LP Connect:http://192.168.250.9:8088 / signalr / connect?clientProtocol = 1.4 &transport = longPolling&connectData = [{ “名称”: “配置”}]&connectionToken = AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2BY%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D 14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - 自动:无法连接到使用传输longPolling . System.TimeoutException:传输超时尝试连接14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - 断开连接14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - Transport.Dispose(708038b9-66b0 -4cc2-95ac-de45411e453c)14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - 已关闭

得到这个错误3次之后我也得到了这个:

14:14:24.0350106 - null - OnError(System.TimeoutException:客户端自2015年5月31日14:13:27以来一直处于非活动状态,并且已超过00:00:50的非活动超时 . 停止连接 . )

2 回答

  • 1

    我成功地做了以下更改:

    1)让客户端和服务器在断开连接(客户端)或处理(服务器)之前有时间完成

    服务器:

    public void ChangePort(int port)
    {
        Console.WriteLine("Received ChangePort request: " + port);
        Task.Delay(500).ContinueWith(_ => _restart(port));
    }
    

    客户:

    _configurationProxy.Invoke("ChangePort", port)
        .ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Console.WriteLine("ChangePort faulted: {0}", task.Exception.Flatten()); 
            }
    
            _hubConnection.Stop();
            _hubConnection.Dispose();
            _hubConnection = null;
        }).Wait();
    

    2)似乎有一个问题, DependencyResolver 在服务器配置期间被处置并且永远不会回来(参见这个GitHub issue) . This SO question描述了您遇到的非常类似的问题 . 这是解决方案:

    internal class Startup
        {
            public Func<int, string> Restart;
    
            public void Configuration(IAppBuilder app)
            {
                var resolver = new DefaultDependencyResolver();
                resolver.Register(typeof(ConfigurationHub), () => new ConfigurationHub(Restart));
    
                var hubConfiguration = new HubConfiguration
                {
                    EnableJSONP = true,
                    EnableDetailedErrors = true,
                    Resolver = resolver
                };
    
                app.MapSignalR(hubConfiguration);
            }
        }
    
  • 0

    我们有类似的情况!而不是更改端口,我需要重新启动客户端或服务器端 .

    我使用一个计时器和一个全局标志'连接'来实现目的 . 例如如果没有连接,那么尝试每5秒钟在客户端捕获一次连接功能,通常在第二次尝试时都没问题,除非没有连接,它通常在第一次尝试失败,因为重启服务器不够快!

相关问题