最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - signalR web application with console examples - Stack Overflow

programmeradmin2浏览0评论

Is there any example of how to use SignalR with a console application ? I have read the wiki but i couldn't manage to run my application i'll show you what i did

server:

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

Hub Class:

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

Client:

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

I'm always getting could not connect alert is there any logic error in my code ?

Is there any example of how to use SignalR with a console application ? I have read the wiki but i couldn't manage to run my application i'll show you what i did

server:

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

Hub Class:

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

Client:

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

I'm always getting could not connect alert is there any logic error in my code ?

Share Improve this question edited Apr 10, 2013 at 19:40 Josiah Ruddell 29.8k8 gold badges67 silver badges68 bronze badges asked Mar 18, 2013 at 11:02 SoraSora 2,55119 gold badges77 silver badges151 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Please make sure you use the same SignalR version on both server and client - and please use 1.0 or 1.01, not 0.5x.

This sample shows how to do it in self hosting and "it works on my machine" https://github./ChristianWeyer/SignalR-SimpleChat-NOUG

HTH.

I had a similar problem when I setup a cross domain example. Here is what I had to do to fix it.

On the client-side

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

    });
});

On the server-side (see here for more detailed server instructions)

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

评论列表(0)

  1. 暂无评论