signalR web应用程序和控制台示例



是否有任何如何使用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>

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

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

这个例子展示了如何在self - hosting和"it works on my machine"中做到这一点https://github.com/ChristianWeyer/SignalR-SimpleChat-NOUG

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.');
        });
    });
});

服务器端

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

相关内容

  • 没有找到相关文章

最新更新