【发布时间】:2014-05-20 12:26:19
【问题描述】:
设置:
- SignalRServer 控制台应用程序:Microsoft.AspNet.SignalR.SelfHost v2.0.3
- SignalRClient 控制台应用程序:Microsoft.AspNet.SignalR.Client v2.0.3
- .NET 4.5.1
我执行以下操作:
- 在客户端按回车键,服务器和客户端再次收到一条消息
- 通过点击服务器控制台中的任意键来处理服务器
- 在服务器控制台中按任意键重新启动服务器
- 客户端重新连接
- 在客户端按回车键,服务器收到消息,但再也没有到达客户端
我希望客户端再次收到该消息。我怀疑这与自托管有关,因为我尝试在运行 IIS 的 Web 应用程序中针对同一个集线器运行客户端并成功。
有什么想法吗?
更新:如果服务器控制台进程被杀死并重新启动,它能够重新连接并再次检索消息。
服务器代码
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
namespace SignalRServer
{
internal class Program
{
private static void Main(string[] args)
{
const string url = "http://localhost:8081";
while (true)
{
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Server running on {0}. Hit any key to stop.", url);
Console.ReadLine();
}
Console.WriteLine("Server stopped");
Console.WriteLine("Hit any key to restart, Esc to exit");
ConsoleKeyInfo ki = Console.ReadKey(true);
if (ki.Key == ConsoleKey.Escape)
return;
}
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
public class AuthenticationHub : Hub
{
public void BroadcastMessageToAll(string message)
{
Clients.All.sendMessageToClient(message);
Console.WriteLine("sendMessageToClient: " + message);
}
public override Task OnConnected()
{
Console.WriteLine("OnConnected " + Context.ConnectionId);
return base.OnConnected();
}
public override Task OnReconnected()
{
Console.WriteLine("OnReconnected " + Context.ConnectionId);
return base.OnReconnected();
}
public override Task OnDisconnected()
{
Console.WriteLine("OnDisconnected " + Context.ConnectionId);
return base.OnReconnected();
}
}
}
客户端代码
using System;
using Microsoft.AspNet.SignalR.Client;
namespace SignalRClient
{
class Program
{
static void Main(string[] args)
{
while (true)
{
var hubConnection = new HubConnection("http://localhost:8081/signalr/hubs");
hubConnection.Closed += () => Console.WriteLine("Closed");
hubConnection.StateChanged += e => Console.WriteLine("StateChanged: " + e.OldState + " " + e.NewState);
var hubProxy = hubConnection.CreateHubProxy("AuthenticationHub");
hubProxy.On<string>("sendMessageToClient",
info => Console.WriteLine("sendMessageToClient received: " + info));
hubConnection.Start();
Console.WriteLine("Client started - hit Enter to send a message - ESC to stop");
Console.ReadKey();
while (true)
{
var keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Escape)
break;
var message = "Console client : " + DateTime.Now.ToString("HH:mm:ss-fff");
hubProxy.Invoke("BroadcastMessageToAll", message);
Console.WriteLine("Client sent BroadcastMessageToAll: " + message);
}
Console.WriteLine("Client stopping");
hubConnection.Stop();
Console.WriteLine("Client stopped - enter any key start again");
Console.ReadLine();
}
}
}
}
【问题讨论】:
-
我希望我能提供更多帮助,但我将您的服务器和客户端代码逐字复制到两个控制台应用程序项目中。安装必要的 NuGet 包后,我能够重新启动服务器应用程序,让客户端自动重新连接,然后继续发送和接收消息。
-
我更新了重现的步骤,使其更加准确。您应该重新启动服务器控制台应用程序本身,但 SignalR 服务器应用程序本身。