在服务器退出时断开客户端
Disconnect clients on server exit
这是我使用的服务器代码:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using System.Collections.Generic;
namespace flar3server
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8080/";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
[HubName("flar3hub")]
public class flare3hub : Hub
{
static Dictionary<string, ChatConnection> connections = new Dictionary<string, ChatConnection>();
Dictionary<string, string> registeredUsers = new Dictionary<string, string>()
{
{ "test1", "pass1" },
{ "test2", "pass2" },
};
/*
public string Send(string message)
{
return message;
}
*/
public void Authenticate(string login, string password)
{
Console.WriteLine("Login [" + Context.ConnectionId + "] " + login + ":" + password);
foreach (ChatConnection connection in connections.Values)
{
if (connection.getLogin() == login)
{
Clients.Caller.Action("ERROR: User already logged in.");
return;
}
}
if (!registeredUsers.ContainsKey(login) || registeredUsers[login] != password)
{
Clients.Caller.Action("ERROR: Login attempt failed.");
return;
}
connections[Context.ConnectionId] = new ChatConnection(login);
Clients.Caller.Action("Logged in successfully");
Clients.All.Action(login + " joined the channel.");
}
public void Broadcast(string message)
{
try
{
Clients.All.sendMessage(connections[Context.ConnectionId].getLogin(), message);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Unpaired [" + Context.ConnectionId + "] " + message);
}
}
}
}
}
这是客户端代码:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Client;
namespace flar3client_cli
{
internal class flar3client_cli
{
private static void onDisconnected()
{
Console.WriteLine("Remote server closed the connection. Press enter to close the application.");
Console.ReadLine();
System.Environment.Exit(1);
}
private static void Main(string[] args)
{
//Set connection
var connection = new HubConnection("http://localhost:8080/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("flar3hub");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}",
task.Exception.GetBaseException());
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
connection.Stop();
System.Environment.Exit(1);
}
else
{
Console.WriteLine("Connected");
}
}).Wait();
connection.Closed += onDisconnected;
myHub.On<string>("Action", param =>
{
Console.WriteLine(param);
});
myHub.On<string>("SendMessage", param =>
{
Console.WriteLine(param);
});
myHub.Invoke<string>("Authenticate", "test1", "pass1").Wait();
while (true)
{
myHub.Invoke<string>("Broadcast", Console.ReadLine()).Wait();
}
如何让服务器在其应用程序 window 关闭时断开所有客户端的连接,以便客户端可以发现?
首先,我禁用了winform的x按钮。然后我在 winform 上添加了一个启动按钮来启动 signalr 服务器。我还添加了一个停止按钮,当这个停止按钮被点击时,我调用 clients.all.somefunction 告诉所有客户端服务器将要关闭,这个过程可能需要几秒钟,我在服务器,说 10 秒,10 秒后,我关闭 winform!我就是这样做的。
但实际上我不知道如何使用控制台服务器来做到这一点。
如何让服务器在其应用程序 window 关闭时断开所有客户端,以便客户端可以发现?
使用您的代码,客户端会发现服务器已消失,但只有在 断开连接超时 之后,客户端才尝试重新建立连接直到放弃。
如果需要,您可以 change timeout value 但最好向所有客户端发送 "hey, server is going down" 消息...
这是我使用的服务器代码:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using System.Collections.Generic;
namespace flar3server
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8080/";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
[HubName("flar3hub")]
public class flare3hub : Hub
{
static Dictionary<string, ChatConnection> connections = new Dictionary<string, ChatConnection>();
Dictionary<string, string> registeredUsers = new Dictionary<string, string>()
{
{ "test1", "pass1" },
{ "test2", "pass2" },
};
/*
public string Send(string message)
{
return message;
}
*/
public void Authenticate(string login, string password)
{
Console.WriteLine("Login [" + Context.ConnectionId + "] " + login + ":" + password);
foreach (ChatConnection connection in connections.Values)
{
if (connection.getLogin() == login)
{
Clients.Caller.Action("ERROR: User already logged in.");
return;
}
}
if (!registeredUsers.ContainsKey(login) || registeredUsers[login] != password)
{
Clients.Caller.Action("ERROR: Login attempt failed.");
return;
}
connections[Context.ConnectionId] = new ChatConnection(login);
Clients.Caller.Action("Logged in successfully");
Clients.All.Action(login + " joined the channel.");
}
public void Broadcast(string message)
{
try
{
Clients.All.sendMessage(connections[Context.ConnectionId].getLogin(), message);
}
catch (KeyNotFoundException)
{
Console.WriteLine("Unpaired [" + Context.ConnectionId + "] " + message);
}
}
}
}
}
这是客户端代码:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Client;
namespace flar3client_cli
{
internal class flar3client_cli
{
private static void onDisconnected()
{
Console.WriteLine("Remote server closed the connection. Press enter to close the application.");
Console.ReadLine();
System.Environment.Exit(1);
}
private static void Main(string[] args)
{
//Set connection
var connection = new HubConnection("http://localhost:8080/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("flar3hub");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}",
task.Exception.GetBaseException());
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
connection.Stop();
System.Environment.Exit(1);
}
else
{
Console.WriteLine("Connected");
}
}).Wait();
connection.Closed += onDisconnected;
myHub.On<string>("Action", param =>
{
Console.WriteLine(param);
});
myHub.On<string>("SendMessage", param =>
{
Console.WriteLine(param);
});
myHub.Invoke<string>("Authenticate", "test1", "pass1").Wait();
while (true)
{
myHub.Invoke<string>("Broadcast", Console.ReadLine()).Wait();
}
如何让服务器在其应用程序 window 关闭时断开所有客户端的连接,以便客户端可以发现?
首先,我禁用了winform的x按钮。然后我在 winform 上添加了一个启动按钮来启动 signalr 服务器。我还添加了一个停止按钮,当这个停止按钮被点击时,我调用 clients.all.somefunction 告诉所有客户端服务器将要关闭,这个过程可能需要几秒钟,我在服务器,说 10 秒,10 秒后,我关闭 winform!我就是这样做的。
但实际上我不知道如何使用控制台服务器来做到这一点。
如何让服务器在其应用程序 window 关闭时断开所有客户端,以便客户端可以发现?
使用您的代码,客户端会发现服务器已消失,但只有在 断开连接超时 之后,客户端才尝试重新建立连接直到放弃。 如果需要,您可以 change timeout value 但最好向所有客户端发送 "hey, server is going down" 消息...