带控制台示例的signalR Web应用程序

时间:2013-03-18 11:02:19

标签: javascript cross-domain console-application signalr

是否有任何关于如何将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>

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

2 个答案:

答案 0 :(得分:1)

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

此示例显示如何在自托管中执行此操作并“在我的计算机上运行” https://github.com/ChristianWeyer/SignalR-SimpleChat-NOUG

HTH。

答案 1 :(得分: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.');
        });

    });
});

在服务器端(有关更详细的服务器说明,请参阅here

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // Don't forget to enable cross domaign
        app.MapHubs(new HubConfiguration { 
            EnableCrossDomain = true 
        });
    }
}