What I was missing was using SignalR with cross-domain .
In the global file, I changed the code:
protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHubs(new HubConfiguration() { EnableCrossDomain = true }); }
It is important to note that since I use a cross-domain, I have a code in the Application_BeginRequest function that should cancel it when SignalR done SignalR request otherwise it does not work . So I canceled it like this:
protected void Application_BeginRequest(object sender, EventArgs e) { //Here is testing whether this request SignalR, if not I do the following code if (HttpContext.Current.Request.Path.IndexOf("signalr") == -1) { HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, x-requested-with"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } } }
On the client:
I added SignalR and SignalR scripts, and I deleted this script:
<script src="/signalr/hubs" type="text/javascript"></script>
Because cross-domain calls are not needed.
The connection is in the following function:
var connection; var contosoChatHubProxy; function RegisterToServiceMessege() { connection = $.hubConnection(); connection.url = 'http://localhost:xxx/signalr'; contosoChatHubProxy = connection.createHubProxy('ChatHub'); //This part happens when function broadcastMessage is activated(from the server) contosoChatHubProxy.on('broadcastMessage', function (userName, message) { alert('You have a messege:\n' + userName + ' ' + message); }); connection.start() .done(function () { console.log('Now connected, connection ID=' + connection.id); }) .fail(function () { console.log('Could not connect'); }); }
Chat.cs on the server:
[HubName("ChatHub")] public class Chat : Hub { [HubMethodName("Send")] public void Send(string name, string message) {
Calling the Send function from one of the clients:
function SendMessege() { contosoChatHubProxy.invoke('Send', 'aaa', 'bbb').done(function () { console.log('Invocation of NewContosoChatMessage succeeded'); }).fail(function (error) { console.log('Invocation of NewContosoChatMessage failed. Error: ' + error); }); }
All customers receive a sent message.
(You can verify this by running multiple browsers at the same time.)