Can't connect to blazor server from console app
intech-paul opened this issue · 1 comments
I have blazor sample working, and can start chats etc, but I am wanting to connect a console app to the same chat hub on the same pc, but cannot.
Here is the sample code
` private static void Main(string[] args)
{
Console.WriteLine("Starting App...");
//Set connection
//var connection = new HubConnection("http://127.0.0.1:6836/");
var connection = new HubConnection("http://localhost:6836");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("/ChatHub");
//Start connection
Console.WriteLine("Starting Connection");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Sleeping...");
Thread.Sleep(1000);
}
myHub.On<string>("addMessage", param => {
Console.WriteLine(param);
});
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();
for (int i = 0; i < 5;i++)
{
Console.WriteLine("Sleeping...");
Thread.Sleep(1000);
}
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);
}
});
`
The error is
There was an error opening the connection:Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: ?. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.<>c.b__0_1(String raw) in //src/Microsoft.AspNet.SignalR.Client/Transports/TransportHelper.cs:line 43
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass31_02.<Then>b__0(Task
1 t) in //src/Microsoft.AspNet.SignalR.Core/TaskAsyncHelper.cs:line 494
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners2.<>c__DisplayClass3_0.<RunTask>b__0(Task
1 t) in /_/src/Microsoft.AspNet.SignalR.Core/TaskAsyncHelper.cs:line 1280
Any clues as to what I am doing wrong ?
OK, first I'd suggest using an async Task Main()
so that your code can use async/await - this simplifies using async code a lot.
Secondly I think the version of the client might be out of date, I tried copying your code but the methods didn't match the ones in the current SignalR client.
However the simplest answer is that the Shared project's ChatClient is almost a re-usable client for any type of client. It used to require the NavigationManger
in the constructor to create the URL, but by changing this to just a string means the console app can also use this.
I created a sample application in this branch: https://github.com/conficient/BlazorChatSample/tree/console-app-sample
If you set both the console and the web as the startup projects you can cross-communicate between console and browser.