How to configure C # winforms application to host SignalR hubs?

I read the SignalR docs and watched a few videos, however I cannot get SignalR to be hosted in the winforms application.

I tried using the source code from the SignalR wiki page: https://github.com/SignalR/SignalR/wiki/Self-host

If you look at the "Complete Sample - Hubs", what is a "server" variable? I don’t understand how it works or how to convert it to C #. According to the wiki, SelfHost implementation is built by default on HttpListener and can be hosted in any type of application (console, Windows service, etc.).

I would like to host SignalR in C # and use it in asp.net. Can anyone shed some light on this for me?

+4
source share
2 answers

For this to work for C # and ASP.NET, I had to use "Cross Domain".

In the JavaScript I used:

<script type="text/javascript" src='http://localhost:8081/signalr/hubs'></script> 

and added:

 $.connection.hub.url = 'http://localhost:8081/signalr' 
+2
source

The wiki sample works great.

Please install the SignalR.Hosting.Self package using NuGet (package manager console)

SignalR.Hosting.Self installation package

Server lives in the SignalR.Hosting.Self namespace.

Example

Console application

 using System; namespace MyConsoleApplication { static class Program { static void Main(string[] args) { string url = "http://localhost:8081/"; var server = new SignalR.Hosting.Self.Server(url); // Map the default hub url (/signalr) server.MapHubs(); // Start the server server.Start(); Console.WriteLine("Server running on {0}", url); // Keep going until somebody hits 'x' while (true) { ConsoleKeyInfo ki = Console.ReadKey(true); if (ki.Key == ConsoleKey.X) { break; } } } public class MyHub : SignalR.Hubs.Hub { public void Send(string message) { Clients.addMessage(message); } } } } 

Asp.NET/Javascript

 <script type="text/javascript" src="Scripts/jquery-1.7.2.js"></script> <script src="/Scripts/jquery.signalR.js" type="text/javascript"></script> <script src="http://localhost:8081/signalr"></script> <script type="text/javascript"> $(function () { // create signalr hub connection myHub= $.connection.myHub; // start hub connection and call the send method $.connection.hub.start(function () { myHub.Send('Hello'); }); }); </script> 

Leave a comment if you have additional answers

+3
source

All Articles