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

Oberon picture Oberon · Jun 20, 2013 · Viewed 14.5k times · Source

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

Answer

Alexander Schmidt picture Alexander Schmidt · Dec 18, 2013

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

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