在服务器出口断开客户端连接
本文关键字:客户端 连接 断开客户 断开 服务器 出口 | 更新日期: 2023-09-27 17:53:59
下面是我使用的服务器代码:
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();
}
我如何使服务器断开所有客户端当它的应用程序窗口关闭,以便客户端可以发现?
如何使服务器在其应用程序窗口关闭时断开所有客户端,以便客户端可以发现这一点?
在你的代码中,客户端会发现服务器已经不在了,但只有在断开连接超时之后,这是客户端试图重新建立连接直到放弃的时间。如果需要的话,你可以改变超时值,但最好是发送"嘿,服务器down了"的消息给所有客户端。首先,禁用了winform的x按钮。然后我在winform上添加了一个启动按钮来启动信号服务器。我还添加了一个停止按钮,当这个停止按钮被点击时,我调用clients.all.somefunction来告诉所有的客户端服务器将被关闭,这个过程可能需要几秒钟,我在服务器中启动一个计时器,比如10秒,10秒后,我关闭winform!我就是这么做的。
但是实际上我不知道如何使用控制台服务器。