Javascript signarweb应用程序及控制台示例

Javascript signarweb应用程序及控制台示例,javascript,cross-domain,console-application,signalr,Javascript,Cross Domain,Console Application,Signalr,有没有关于如何在控制台应用程序中使用SignalR的示例? 我已经阅读了维基,但我无法运行我的应用程序。我将向您展示我所做的 服务器: 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 Mi

有没有关于如何在控制台应用程序中使用SignalR的示例? 我已经阅读了维基,但我无法运行我的应用程序。我将向您展示我所做的

服务器:

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();
         }
    }
}
客户:

<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>

信号客户端
$(函数(){
//动态创建的代理
var myHub=$.connection.chatHub;
//启动连接
$.connection.hub.url='0http://localhost:8083/signalr';
$.connection.hub.start().done(函数(){
警报(“现已连接!”);
}).fail(函数(){
警报(“无法连接!”);
});
});

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

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

    此示例显示了如何在自托管和“它在我的机器上工作”中执行此操作


    HTH.

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

    在客户端

    $(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.');
            });
    
        });
    });
    
    在服务器端(有关更详细的服务器说明,请参阅)


    另外,在Signal源代码中,您可以查看示例,其中有一个自托管服务器示例和一个控制台应用程序示例。我在您提供的链接中下载了您的源代码,但当我尝试测试它时,visual studio说您的项目版本不兼容,因此我无法测试它,我有VS 2010版本
    $(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 
            });
        }
    }