简体   繁体   English

SignalR WPF服务器和ASP.NET MVC问题

[英]SignalR wpf server and ASP.NET MVC issue

I have in WPF SignalR server 我在WPF SignalR服务器中

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        Closing += (s, e) => ViewModelLocator.Cleanup();
        StartOptions options = new StartOptions();

        options.Urls.Add("http://localhost:8080");
        using (WebApp.Start(options))
        {
        }
    }
}

Hub.cs Hub.cs

[HubName("clientPushHub")]
public class TestHub : Hub
{
    public string GetServerTime()
    {
        return DateTime.UtcNow.ToString();
    }

    public void Heartbeat()
    {
        Console.WriteLine("Hub Heartbeat\n");
        Clients.All.heartbeat();
    }

    public Task JoinGroup(string groupId)
    {
        return Groups.Add(Context.ConnectionId, groupId);
    }

    public void addHit(string pageId)
    {

    }
    public Task LeaveGroup(string groupId)
    {
        return Groups.Remove(Context.ConnectionId, groupId);
    }

    public override Task OnConnected()
    {
        return (base.OnConnected());
    }
}

Startup.cs 启动文件

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        PerformanceEngine performanceEngine = new PerformanceEngine(1000);
        Task.Factory.StartNew(async () => await performanceEngine.OnPerformanceMonitor());
        // map SignalR urls
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);

            var hubConfiguration = new HubConfiguration
            {
                EnableDetailedErrors = true
            };
            map.RunSignalR(hubConfiguration);
        }
        );
    }
}

And in my ASP.NET MVC client I have Index.cshtml 在我的ASP.NET MVC客户端中,我具有Index.cshtml

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script type="text/javascript" src="~/signalr/hubs"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/app.js"></script>

<body ng-app="signalRIntegrationApp">
    <div ng-controller="ServerTimeController">
        <div>
            <h3>Time from server (being pushed every 5 seconds):</h3>
            <h4>{{currentServerTime}}</h4>
        </div>

        </div>
</body>

and in app.js 并在app.js中

(function () {
    var app = angular.module("signalRIntegrationApp", []);
    app.service('signalRSvc', function ($rootScope) {
        var proxy = null;

        var initialize = function () {
            var connection = $.hubConnection("http://localhost:8080");
            proxy = connection.createHubProxy('clientPushHub');

            connection.start(function () {
                proxy.addHit();
            });

            proxy.on('serverTime', function (data) {
                    $rootScope.$emit('serverTime', data);
            });
        }

        return {
            initialize: initialize
        };
    });

    app.controller('ServerTimeController', ["$scope", 'signalRSvc', function($scope, signalRSvc) {
        var vm = this;

        $scope.$on("serverTime", function (e, data) {
            $scope.$apply(function () {
                vm.currentServerTime = data;
            });
        }); 
        signalRSvc.initialize();
    }]);
})()

When I start the client in Firefox I get the error on 在Firefox中启动客户端时,出现以下错误 错误

The client is on http://localhost:8081 on IIS Express 客户端位于IIS Express上的http:// localhost:8081

Update: Error during negotiation request 更新:协商请求期间出错

In this part of code 在这部分代码中

 using (WebApp.Start(options))
 {
 }

You create and then immediately dispose server object. 您创建然后立即处置服务器对象。 It means that you shut down server immediately after creation. 这意味着您在创建后立即关闭服务器。

You should store reference to that object and call dispose when needed. 您应该存储对该对象的引用,并在需要时调用dispose。 Or just store object and let OS handle resource cleanup when process exits. 或者只是存储对象,让OS在进程退出时处理资源清理。

Documentation 文献资料

Relevant part: 相关部分:

Return Value 返回值

Type: System.IDisposable 类型:System.IDisposable

An IDisposible instance that can be called to shut down the web app. 一个IDisposible实例,可以调用它来关闭Web应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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