SignalR-console-app starten muss aufgerufen werden, bevor Daten gesendet werden können
Ich versuche einen client erstellen signalr-app. Derzeit bin ich in der Lage, mein MVC-clients zum senden von Nachrichten zu und von den Hub, aber ich bin vor einige Probleme mit meinem .NET-client-app
Server-Hub-code:
namespace ServerHub
{
public class ChatterBox : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}
}
Console App-code (fast direkt gehoben von github)
using Microsoft.AspNet.SignalR.Client.Hubs;
using ServerHub;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Say("hello from console");
}
public static void Say(string message)
{
//var connection = new HubConnection("http://localhost/");
//IHubProxy myHub = connection.CreateHubProxy("ChatterBox");
var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
//Do more stuff here
}
});
connection.Send("Hello").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success");
}
});
}
}
}
Bekomme ich die Fehlermeldung unten, während connection.Send()
Starten muss aufgerufen werden, bevor Daten gesendet werden können.
Wo habe ich bloß falsch gemacht?
EDIT:
2. Versuch:
var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}",
connection.ConnectionId);
//Do more stuff here
connection.Send("Hello");
}
});
myHub.Invoke("Send", "lol");
Immer noch gibt mir eine Fehlermeldung
EDIT: 3. Versuch
var connection = new HubConnection("http://localhost:60610/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
//Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
myHub.Invoke("Send", "lol");
}
});
Scheint zu funktionieren
Brauchte ich auch, um die port-Nummer, z.B., wenn ich bin Browsen auf meiner MVC-website wie diese: http://localhost:60610/
dies ist die Adresse, die ich brauche, um in meiner console app.
Kann ich sagen, dass während der Bereitstellung, es wird nicht ein Problem sein, weil es standardmäßig port 80?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Lesen Sie diese http://msdn.microsoft.com/en-us/library/dd460717.aspx
TL;DR Start ist async und das ist nicht, wie Sie schreiben, sequentiell asynchronen code:
Wenn Sie verwenden können .NET 4.5, es wird Ihr Leben viel einfacher:
UPDATE: Lasst uns versuchen, diese wieder. Der code unten funktionieren wird, aber Sie sollten wirklich Lesen Sie diesen link, um zu lernen, wie zu schreiben async-code. Ansonsten wirst du einfach diese Probleme über und über.
connection.Send("Hello");
stattconnection.Send("Hello").ContinueWith(task => { ....... });
Aber es hat nicht funktioniert, keine Fehler msg entweder. Nichts war an meinem browser