简体   繁体   中英

SignalR wpf server and ASP.NET MVC issue

I have in WPF SignalR server

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

[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

<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

(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 错误

The client is on http://localhost:8081 on IIS Express

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. Or just store object and let OS handle resource cleanup when process exits.

Documentation

Relevant part:

Return Value

Type: System.IDisposable

An IDisposible instance that can be called to shut down the web app.

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