Is there a small example of a console or winform app using signalR to send a message to a .net hub?. I have tried the .net examples and have looked at the wiki but it is not making sense to me the relationship between the hub(.net) and client(console app) (could not find an example of this). Does the app just need the address and name of hub to connect?.
If someone could provide a small tidbit of code showing the app connecting to a hub and sending "Hello World" or something that the .net hub receives?.
PS. I have a standard hub chat example which works well , if I try to assign a hub name in Cs to it , it stops working i.e [HubName("test")] , do you know the reason for this?.
Thanks.
Current Console app Code.
static void Main(string[] args)
{
//Set connection
var connection = new HubConnection("http://localhost:41627/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateProxy("chat");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Connected");
}
}).Wait();
//connection.StateChanged += connection_StateChanged;
myHub.Invoke("Send", "HELLO World ").ContinueWith(task => {
if(task.IsFaulted)
{
Console.WriteLine("There was an error calling send: {0}",task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Send Complete.");
}
});
}
Hub Server. (different project workspace)
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.addMessage(message);
}
}
Info Wiki for this is http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client
First of all, you should install SignalR.Host.Self on the server application and SignalR.Client on your client application by nuget :
PM> Install-Package SignalR.Hosting.Self -Version 0.5.2
PM> Install-Package Microsoft.AspNet.SignalR.Client
Then add the following code to your projects ;)
(run the projects as administrator)
Server console app:
using System;
using SignalR.Hubs;
namespace SignalR.Hosting.Self.Samples {
class Program {
static void Main(string[] args) {
string url = "http://127.0.0.1:8088/";
var server = new 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;
}
}
}
[HubName("CustomHub")]
public class MyHub : Hub {
public string Send(string message) {
return message;
}
public void DoSomething(string param) {
Clients.addMessage(param);
}
}
}
}
Client console app:
using System;
using SignalR.Client.Hubs;
namespace SignalRConsoleApp {
internal class Program {
private static void Main(string[] args) {
//Set connection
var connection = new HubConnection("http://127.0.0.1:8088/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("CustomHub");
//Start connection
connection.Start().ContinueWith(task => {
if (task.IsFaulted) {
Console.WriteLine("There was an error opening the connection:{0}",
task.Exception.GetBaseException());
} else {
Console.WriteLine("Connected");
}
}).Wait();
myHub.Invoke<string>("Send", "HELLO World ").ContinueWith(task => {
if (task.IsFaulted) {
Console.WriteLine("There was an error calling send: {0}",
task.Exception.GetBaseException());
} else {
Console.WriteLine(task.Result);
}
});
myHub.On<string>("addMessage", param => {
Console.WriteLine(param);
});
myHub.Invoke<string>("DoSomething", "I'm doing something!!!").Wait();
Console.Read();
connection.Stop();
}
}
}
Example for SignalR 2.2.1 (May 2017)
Server
Install-Package Microsoft.AspNet.SignalR.SelfHost -Version 2.2.1
[assembly: OwinStartup(typeof(Program.Startup))]
namespace ConsoleApplication116_SignalRServer
{
class Program
{
static IDisposable SignalR;
static void Main(string[] args)
{
string url = "http://127.0.0.1:8088";
SignalR = WebApp.Start(url);
Console.ReadKey();
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
/* CAMEL CASE & JSON DATE FORMATTING
use SignalRContractResolver from
https://stackoverflow.com/questions/30005575/signalr-use-camel-case
var settings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc
};
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);
*/
app.MapSignalR();
}
}
[HubName("MyHub")]
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
}
Client
(almost the same as Mehrdad Bahrainy reply)
Install-Package Microsoft.AspNet.SignalR.Client -Version 2.2.1
namespace ConsoleApplication116_SignalRClient
{
class Program
{
private static void Main(string[] args)
{
var connection = new HubConnection("http://127.0.0.1:8088/");
var myHub = connection.CreateHubProxy("MyHub");
Console.WriteLine("Enter your name");
string name = Console.ReadLine();
connection.Start().ContinueWith(task => {
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Connected");
myHub.On<string, string>("addMessage", (s1, s2) => {
Console.WriteLine(s1 + ": " + s2);
});
while (true)
{
Console.WriteLine("Please Enter Message");
string message = Console.ReadLine();
if (string.IsNullOrEmpty(message))
{
break;
}
myHub.Invoke<string>("Send", name, message).ContinueWith(task1 => {
if (task1.IsFaulted)
{
Console.WriteLine("There was an error calling send: {0}", task1.Exception.GetBaseException());
}
else
{
Console.WriteLine(task1.Result);
}
});
}
}
}).Wait();
Console.Read();
connection.Stop();
}
}
}
To build on #dyslexicanaboko's answer for dotnet core, here is a client console application:
Create a helper class:
using System;
using Microsoft.AspNetCore.SignalR.Client;
namespace com.stackoverflow.SignalRClientConsoleApp
{
public class SignalRConnection
{
public async void Start()
{
var url = "http://signalr-server-url/hubname";
var connection = new HubConnectionBuilder()
.WithUrl(url)
.WithAutomaticReconnect()
.Build();
// receive a message from the hub
connection.On<string, string>("ReceiveMessage", (user, message) => OnReceiveMessage(user, message));
var t = connection.StartAsync();
t.Wait();
// send a message to the hub
await connection.InvokeAsync("SendMessage", "ConsoleApp", "Message from the console app");
}
private void OnReceiveMessage(string user, string message)
{
Console.WriteLine($"{user}: {message}");
}
}
}
Then implement in your console app's entry point:
using System;
namespace com.stackoverflow.SignalRClientConsoleApp
{
class Program
{
static void Main(string[] args)
{
var signalRConnection = new SignalRConnection();
signalRConnection.Start();
Console.Read();
}
}
}
The Self-Host now uses Owin. Checkout http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-signalr-20-self-host to setup the server. It's compatible with the client code above.
This is for dot net core 2.1 - after a lot of trial and error I finally got this to work flawlessly:
var url = "Hub URL goes here";
var connection = new HubConnectionBuilder()
.WithUrl($"{url}")
.WithAutomaticReconnect() //I don't think this is totally required, but can't hurt either
.Build();
//Start the connection
var t = connection.StartAsync();
//Wait for the connection to complete
t.Wait();
//Make your call - but in this case don't wait for a response
//if your goal is to set it and forget it
await connection.InvokeAsync("SendMessage", "User-Server", "Message from the server");
This code is from your typical SignalR poor man's chat client. The problem that I and what seems like a lot of other people have run into is establishing a connection before attempting to send a message to the hub. This is critical, so it is important to wait for the asynchronous task to complete - which means we are making it synchronous by waiting for the task to complete.
Related
I'm doing an application using server streaming.
The problem is the client doesn't read the data from the server streaming.
This is my proto service:
service UserService {
rpc GetData(Id) returns (stream DataResponse) {}
}
message Id {
int32 id = 1;
}
message DataResponse {
bytes data = 1;
}
c# server is like this:
public override async Task GetData(Id request, IServerStreamWriter<DataResponse> response, ServerCallContext context)
{
var user = {} // get user
foreach (var d in user.Data)
{
await response.WriteAsync(new DataResponse { Data = d });
}
}
And it works because I have a NodeJS client where I can call the server and works perfectly.
Client in Node is
let call = client.getData({id:1})
call.on('data', function (response) {
// do things
})
call.on('end', function () {
// do things
})
And c# client is:
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id{Id_ = 1});
while(await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop"); // <-- This is never executed
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data}");
}
I've also added a try/catch and it doesn't output anything so it seems MoveNext() is always false.
What is the problem here? Why NodeJS client works and c# client can't read the stream? Have I missed something?
Here is full client.cs class:
class Program
{
const int Port = 50051;
static void Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
GetDataStreaming(client);
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}
private static async void GetDataStreaming(UserService.UserServiceClient client)
{
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id { Id_ = 1 });
while (await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop");
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data.ToStringUtf8()}");
}
}
}
The issue is that your client has ended before the client receive the response. When you call GetDataStreaming(client) in Main it doesn't wait and finishes.
To fix the issue change async void GetDataStreaming to async Task GetDataStreaming.
private static async Task GetDataStreaming(UserService.UserServiceClient client)
{
AsyncServerStreamingCall<DataResponse> response = client.GetData(new Id { Id_ = 1 });
while (await response.ResponseStream.MoveNext())
{
Console.WriteLine("Into while loop");
DataResponse current = response.ResponseStream.Current;
Console.WriteLine($"{current.Data.ToStringUtf8()}");
}
}
Change static void Main to static async Task Main and you should also call channel.ShutdownAsync method at the end.
static async Task Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
await GetDataStreaming(client);
await channel.ShutdownAsync();
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}
Another option is to change async void GetDataStreaming to async Task GetDataStreaming and in Main method wait until Task complete.
static void Main(string[] args)
{
try
{
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new UserService.UserServiceClient(channel);
var task = GetDataStreaming(client);
task.Wait();
}
catch (RpcException ex)
{
Console.WriteLine($"Error: {{Code: {ex.StatusCode}, Status: {ex.Status.Detail}}}");
}
}
I am trying to separate the MassTransit demo (https://masstransit-project.com/MassTransit/quickstart.html) into two separate applications, but my consumer application is not receiving any messages.
I have created 3 projects: Send, Receive, and Messages. Send contains a Send class which is my producer code, Receive contains a Receive class which is my consumer code, and Messages contains classes for my messages.
Here is my Send class:
using MassTransit;
using Messages;
using System;
namespace MassTransitTest
{
class Send
{
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
var host = config.Host(new Uri("rabbitmq://localhost"), c =>
{
c.Username("guest");
c.Password("guest");
});
});
bus.Start();
Console.WriteLine("Publishing message");
bus.Publish(new TextMessage() { Text = "Testing 12345" });
bus.Stop();
Console.ReadLine();
}
}
}
here is my Receive class:
using MassTransit;
using Messages;
using System;
namespace Receive
{
class Receive
{
static void Main(string[] args)
{
var bus = Bus.Factory.CreateUsingRabbitMq(config =>
{
var host = config.Host(new Uri("rabbitmq://localhost"), c =>
{
c.Username("guest");
c.Password("guest");
});
config.ReceiveEndpoint(host, "queue", endpoint =>
{
endpoint.Handler<TextMessage>(context =>
{
return Console.Out.WriteLineAsync($"{context.Message.Text}");
});
});
});
bus.Start();
Console.WriteLine("Receive listening for messages");
Console.ReadLine();
bus.Stop();
}
}
}
Finally, here is my TextMessage class:
using System;
namespace Messages
{
public class TextMessage
{
public string Text { get; set; }
}
}
When I send the message from my Send class I would like to receive it and output it to the console from my Receive class.
I figured it out! Publish is an asynchronous message that returns a Task, so I needed to await and then everything worked. My Send program was exiting before the message was fired off.
I'm a newb to SignalR. I'm trying to set up a Asp.Net Core WebAPI so that other clients can connect to it using SignalR and get real-time data.
My Hub class is:
public class TimeHub : Hub
{
public async Task UpdateTime(string message)
{
await Clients.All.SendAsync("ReceiveMessage", message);
}
}
I have a relay class as follows:
public class TimeRelay : ITimeRelay
{
private readonly IHubContext<TimeHub> _timeHubContext;
public TimeRelay(IHubContext<TimeHub> context)
{
_timeHubContext = context;
Task.Factory.StartNew(async () =>
{
while (true)
{
await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
Thread.Sleep(2000);
}
});
}
}
Startup class:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseSignalR((x) =>
{
x.MapHub<TimeHub>("/timeHub");
});
app.UseMvc();
}
The client is a console application and the code is:
class Program
{
static Action<string> OnReceivedAction = OnReceived;
static void Main(string[] args)
{
Connect();
Console.ReadLine();
}
private static async void Connect()
{
var hubConnectionBuilder = new HubConnectionBuilder();
var hubConnection = hubConnectionBuilder.WithUrl("http://localhost:60211/timeHub").Build();
await hubConnection.StartAsync();
var on = hubConnection.On("ReceiveMessage", OnReceivedAction);
Console.ReadLine();
on.Dispose();
await hubConnection.StopAsync();
}
static void OnReceived(string message)
{
System.Console.WriteLine($"{message}");
}
}
I tried debugging the application. The client got connected to the TimeHub succesfully. The no of connections in Clients.All changed from 0 to 1, when the client got connected. But, when await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString()); is executed, the UpdateTime function in TimeHub is not getting executed and the client is not getting any message.
I tried using "UpdateTime", "SendMessage", and "ReceiveMessage" as method in Clients.All.SendAsync in TimeRelay class. Nothing worked. Could someone point out my mistake in this.
For Clients, it will be null if there is no client connecting to server. For starting Asp.Net Core SignalR and Console App at the same time, the Clients may be null since Index may be called before Console App connects the signalR server.
Try steps below:
Change TimeHub
public class TimeHub: Hub
{
public async Task UpdateTime(string message)
{
if (Clients != null)
{
await Clients.All.SendAsync("ReceiveMessage", message);
}
}
}
Register TimeHub
services.AddSingleton<TimeHub>();
Controller
public class HomeController : Controller
{
private readonly TimeHub _timeHub;
public HomeController(TimeHub timeHub)
{
_timeHub = timeHub;
}
public IActionResult Index()
{
Task.Factory.StartNew(async () =>
{
while (true)
{
try
{
await _timeHub.UpdateTime(DateTime.Now.ToShortDateString());
Thread.Sleep(2000);
}
catch (Exception ex)
{
}
}
});
return View();
}
I got it to work and thought I will answer it here. Thanks #TaoZhou for the tip.
My mistake was sending "UpdateTime" from server and waiting on "ReceiveMessage" at the client.
Ideally the code should look like the following:
SignalR Server:
await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
SignalR Client:
var on = hubConnection.On("UpdateTime", OnReceivedAction);
In this case any message send from the server would be received at the client instantly.
Please refer the code provided in the question for more info.
I need to exchange data between a .NET Console and an ASP.NET Core 2.0 applications. The second already hosts a SignalR server:
public class MyHub : Hub
{
public override async Task OnConnectedAsync()
{
await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} connected");
}
public Task Send(string message)
{
return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
}
}
app.UseSignalR(routes =>
{
routes.MapHub<MyHub>("hubs");
});
from the html pages I can invoke the functions. Hence it's working.
Now I added to the solution a .NET 4.6 Console application:
public static HubConnection _connection;
static void Main(string[] args)
{
_connection = new HubConnection("http://localhost:51278/hubs");
var myHub = _connection.CreateHubProxy("MyHub");
_connection.Closed += OnDisconnected;
OnDisconnected();
myHub.Invoke<string>("Send", "Hello World ").ContinueWith(task => {
if (task.IsFaulted)
{
Console.WriteLine("There was an error calling send: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine(task.Result);
}
});
_connection.Stop();
}
static void OnDisconnected()
{
Console.WriteLine("Disconnected. Try to connect...");
var t = _connection.Start(new LongPollingTransport());
bool result = false;
t.ContinueWith(task =>
{
if (!task.IsFaulted)
{
result = true;
Console.WriteLine("Connected");
}
}).Wait();
if (!result)
{
OnDisconnected();
}
}
But the output is:
Disconnected. Try to connect...
Disconnected. Try to connect...
and nothing else. I'm new to SignalR and I don't know how might debug further such a behavior.
SignalR for.Net Core is not compatible with the previous version of SignalR for the Full .Net framework.
SignalR clients written in the full framework cannot be used with hubs in Asp.Net core (and vice-versa).
Here is confirmation from GitHub
Will a SignalR Core server connect OK with 'traditional' Signalr 2 clients?
Trying to get a simple demo of NetTcpBinding working in order to expand it into another project.
Architecture: 2 console apps (1 host/server, 1 client) and 1 type library project. Both console apps have a reference to the type library project.
Host application:
class Program
{
static void Main()
{
var netTcpBinding = new NetTcpBinding(SecurityMode.None)
{
PortSharingEnabled = true
};
var netTcpAdddress = new Uri("net.tcp://127.0.0.1:1234/HelloWorldService/");
var tcpHost = new ServiceHost(typeof(HelloWorldService), netTcpAdddress);
tcpHost.AddServiceEndpoint(typeof(IHelloWorld), netTcpBinding, "IHelloWorld");
tcpHost.Open();
Console.WriteLine($"tcpHost is {tcpHost.State}. Press enter to close.");
Console.ReadLine();
tcpHost.Close();
}
}
public class HelloWorldService : IHelloWorld
{
public void HelloWorld()
{
Console.WriteLine("Hello World!");
}
public void WriteMe(string text)
{
Console.WriteLine($"WriteMe: {text}");
}
}
Client application:
static void Main()
{
Console.WriteLine("Press enter when the service is opened.");
Console.ReadLine();
var endPoint = new EndpointAddress("net.tcp://127.0.0.1:1234/HelloWorldService/");
var binding = new NetTcpBinding ();
var channel = new ChannelFactory<IHelloWorld>(binding, endPoint);
var client = channel.CreateChannel();
try
{
Console.WriteLine("Invoking HelloWorld on TcpService.");
client.HelloWorld();
Console.WriteLine("Successful.");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
Console.WriteLine("Press enter to quit.");
Console.ReadLine();
}
Type Library:
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
void HelloWorld();
[OperationContract]
void WriteMe(string text);
}
I believe I have all necessary services installed and running:
Obviously I'm trying to do all the config at runtime.
I consistently get this error message on the client:
Invoking HelloWorld on TcpService.
Exception: There was no endpoint listening at
net.tcp://127.0.0.1:1234/HelloWorldService/ that could accept the
message. This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details. Press enter to quit.
Am I missing something obvious?
Your service is exposing the endpoint at address:
net.tcp://127.0.0.1:1234/HelloWorldService/IHelloWorld
but your client is connecting to:
net.tcp://127.0.0.1:1234/HelloWorldService/
You'll also need to set the client NetTcpBinding SecurityMode the same as the server (None).