Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }       
    }
}
like image 502
Oberon Avatar asked Jun 20 '13 17:06

Oberon


People also ask

What platform does SignalR client support?

Windows Desktop and Silverlight Applications In addition to running in a web browser, SignalR can be hosted in standalone Windows client or Silverlight applications.

How many connections can SignalR handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

Can SignalR be used in Web API?

SignalR can be used together with Web API just fine, so some functionality in application which is not realtime and doesn't require persistent connection could be served by Web API.

Which SignalR allows bidirectional communication between server and client?

ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.


2 Answers

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

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

like image 138
Alexander Schmidt Avatar answered Sep 18 '22 10:09

Alexander Schmidt


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.

like image 38
Randall Flagg Avatar answered Sep 20 '22 10:09

Randall Flagg