信号器:asp.net mvc中的客户端连接到外部服务器

时间:2016-01-12 13:14:18

标签: c# asp.net asp.net-mvc-4 signalr

我需要在服务器SignalR上连接我的Web应用程序。 Windows Phone应用程序正在访问此服务器,并且可以正常工作。我需要做的是通过Web应用程序访问并向另一侧连接的这个手机Windows应用程序发送消息。

我想通过Controller进行通信,因为必须在网站屏幕上通知用户。

Server Startup.cs:

D

}

ConnectionHelper:

internal class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            try
            {
                app.UseCors(CorsOptions.AllowAll);

                ConfigureGlobalHost();
                var configuration = new HubConfiguration
                {
                    EnableDetailedErrors = true,
                    EnableJSONP = true
                };

                app.MapSignalR("/signalr", configuration);
            }
            catch (Exception)
            {
                //Log.AddMessage("An error occurred during server configuration: " + ex.Message);
            }
        }

        public void ConfigureGlobalHost()
        {
             GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromMinutes(1);
             GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromMinutes(1);
             GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromMinutes(10);
             GlobalHost.Configuration.DefaultMessageBufferSize = 500;
        }      

控制器:

public class HubConnectionHelper : IDisposable
{
     public IHubProxy HubProxy { get; set; }
     public HubConnection Connection { get; set; }
     private string _serverURI;

     public HubConnectionHelper(string nomeDoHub, string urlServicoCentral)
     {
         _serverURI = string.Format("http://{0}:8080", urlServicoCentral.Split(':')[0]);

         Connection = new HubConnection(_serverURI);
         Connection.TransportConnectTimeout = TimeSpan.FromMinutes(10);

         Connection.Closed += Connection_Closed;

         HubProxy = Connection.CreateHubProxy(nomeDoHub);
     }
}

服务器中心:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(ConfiguracaoTerminalViewModel cTerminal)
{
     if (ModelState.IsValid)
     {
          var loja = _lService.ObterLoja();
          if (_cTerminalService.Gravar(new ConfiguracaoTerminal
          {
               Id = Guid.NewGuid(),
               CaminhoServidorEmissao = cTerminal.CaminhoServidorEmissao,
               NumeroEletronico = cTerminal.NumeroEletronico,
               Serie = cTerminal.Serie,
               LojaId = cTerminal.LojaId,
               TerminalId = cTerminal.TerminalId,
               AmbienteNFe = cTerminal.AmbienteNFe
          }))
          {
               await _cTerminalHub.NotificarTerminal(cTerminal.TerminalId.ToString(),
                                              "Configuração inserida com sucesso.\n\nFim das configurações.");
               return RedirectToAction("Index");
          }
     }
     return View(cTerminal);
}

修改

ConfiguracaoTerminalHub(clientside,用于连接):

public class ConfiguracaoTerminalHub : Hub, IDisposable
{
    private readonly SignalRConnectionService _signalRConnectionService;
    private readonly TerminalService _terminalService;

    public ConfiguracaoTerminalHub()
    {
        _signalRConnectionService = new SignalRConnectionService();
        _terminalService = new TerminalService();
    }



    public void NotificarTerminal(string connectionId, string message)
    {
        Clients.Client(connectionId.ToString()).ReceberNotificacaoConfiguracaoTerminal(message);
        Console.WriteLine("Notificação enviada do Retaguarda para um terminal pendente.");
    }


    public override Task OnConnected()
    {
        SalvarConexao(Guid.Parse(Context.Headers.Get("terminalId")));
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        ExcluirConexao(Guid.Parse(Context.ConnectionId));
        return base.OnDisconnected(stopCalled);
    }

错误:

public class ConfiguracaoTerminalHub : IDisposable
{
    public ConfiguracaoTerminalHub(string urlServidor)
    {
          _conHelper = new HubConnectionHelper("ConfiguracaoTerminalHub", urlServidor);
     }

     public async Task NotificarTerminal(string connectionId, string message)
     {
          await _conHelper.Connection.Start(); //the error occurs here
          await _conHelper.HubProxy.Invoke("NotificarTerminal", connectionId, message);
     }
}

0 个答案:

没有答案