简体   繁体   中英

It's possible signalR client web to connect a server on a different PC?

I have a console application who is the signalR server, on my PC.

I have a html page who is a signalR client on internet. But i try to connect the server but I have a bad request 400 error. If the server is down i have a another reponse.

It's possible or not the signalR client to connect a server on a PC ?

(Sorry for my english)

My page

<input type="text" id="msg" value=" " />
    <input type="button" id="send" value="send" />
    <ul id="message">
    </ul>
    <script src="Scripts/jquery-1.6.4.min.js"></script>
    <script src="Scripts/jquery.signalR-1.1.2.min.js"></script>
    <script>
        $(function () {
            jQuery.support.cors = true;
            // Open a connection to the remote server
            var connection = $.hubConnection('http://MyIpAddress:8080');
            var chat = connection.createHubProxy('chat');

            chat.on('send', function (message) {
                $('#message').append('<li>' + message + '</li>');
            });

            // Turn logging on so we can see the calls in the browser console
            connection.logging = true;

            connection.start().done(function () {
                $('#send').click(function () {
                    chat.invoke('send', $('#msg').val());
                });
            });
        });
    </script>

My Server code

using Microsoft.Owin.Hosting;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

//Install-Package Microsoft.Owin.Hosting -pre
//Install-Package Microsoft.Owin.Host.HttpListener -pre

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

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

Startup.cs

using Microsoft.AspNet.SignalR;
using Owin;

namespace TestServer
{
    public class Startup
    {
        // This method name is important
        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration
            {
                EnableCrossDomain = true
            };

            app.MapHubs(config);
        }
    }
}

Chat.cs

using Microsoft.AspNet.SignalR;
using System;
using System.Threading.Tasks;

namespace TestServer.Hubs
{
    public class Chat : Hub
    {
        public override Task OnConnected()
        {
            Console.WriteLine("OnConnected " + Context.ConnectionId);
            return base.OnConnected();
        }

        public void Send(string message)
        {
            Console.WriteLine(string.Format("Receive: {0} by {1} ", message, Context.ConnectionId));
            Clients.All.send(message);
        }       
    }
}

What about this as the first line in your JS-initialisation:

$.connection.hub.url = "YOUR URL";

I just had this same issue and although it has been some time since the question was published this is how I solved it:

I changed in the server code the url from:

string url = "http://localhost:8080";

to:

string url = "http://*:8080";

or:

string url = "http://+:8080";

This changed the bind scope but for me it solved a few issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM