0

有没有关于如何将 SignalR 与控制台应用程序一起使用的示例?我已经阅读了 wiki,但我无法运行我的应用程序,我会告诉你我做了什么

服务器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System;
using Owin;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Owin.Hosting;


namespace SignalRChat.Server
{

    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8083";

            using (WebApplication.Start<Startup>(url)) {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }

    class Startup
    {
         public void Configuration(IAppBuilder app)
         {
             app.MapHubs();
         }
    }
}

集线器类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalRChat.Server
{
    public class ChatHub : Hub
    {
        public void Send(string message)
        {
            Clients.All.addMessage(message);
        }
    }
}

客户:

<html>
    <head>
    <title>SignalR client</title>

    <script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.signalR-0.5.1.min.js"></script>
    <script type="text/javascript" src="http://localhost:8083/signalr/hubs"></script>

    <script type="text/javascript">
    $(function () {
        // Proxy created on the fly
        var myHub = $.connection.chatHub;
        // Start the connection
        $.connection.hub.url = 'http://localhost:8083/signalr';
        $.connection.hub.start().done(function () {
            alert("Now connected!");
        }).fail(function () {
            alert("Could not Connect!");
        });
    });
    </script>

    </head>
    <body>
        <ul id="messages"></ul>
    </body>
</html>

我总是收到无法连接警报我的代码中是否有任何逻辑错误?

4

2 回答 2

1

请确保您在服务器和客户端上使用相同的 SignalR 版本 - 请使用 1.0 或 1.01,而不是 0.5x。

这个示例展示了如何在自托管和“它在我的机器上工作” https://github.com/ChristianWeyer/SignalR-SimpleChat-NOUG

HTH。

于 2013-03-18T15:29:20.633 回答
0

我在设置跨域示例时遇到了类似的问题。这是我必须做的修复它。

在客户端

$(function () {
    var url = 'http://localhost:8083/signalr';

    // start connection on a different port
    $.connection(url).start().done(function () {
        var someHub = $.connection.someHub;

        $.connection.hub.logging = true;
        $.connection.hub.error(function () {
            console.error('An error occurred with the hub connection.');
        });

        // seems to be a bug in CORs signalR client library that
        // the URL host in the connection object is not passed through to the hub
        $.connection.hub.url = url;

        someHub.client.someFunction = function (message) {
            console.log(message);
        };

        // since you have set the `$.connection.hub.url` this now works
        $.connection.hub.start(function () {
            console.log('Connection Established.');
        });

    });
});

在服务器端(有关更详细的服务器说明,请参见此处)

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // Don't forget to enable cross domaign
        app.MapHubs(new HubConfiguration { 
            EnableCrossDomain = true 
        });
    }
}
于 2013-04-10T19:32:20.760 回答