断开服务器出口上的客户端

时间:2015-06-05 13:53:11

标签: c# signalr

这是我正在使用的服务器代码:

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();
            }

如何在关闭应用程序窗口时让服务器断开所有客户端的连接,以便客户端可以找到它?

2 个答案:

答案 0 :(得分:1)

首先,我禁用了winform的x按钮。然后我在winform上添加了一个启动按钮来启动信号服务器。我还添加了一个停止按钮,当单击此停止按钮时,我调用clients.all.somefunction告诉所有客户端服务器将要关闭,这个过程可能需要几秒钟,我启动一个计时器服务器,比如10秒,10秒后,我关闭了winform!这就是我做到的。

但实际上我不知道如何使用控制台服务器。

答案 1 :(得分:1)

如何让服务器在其应用程序窗口关闭时断开所有客户端的连接,以便客户端可以找到它?

使用您的代码,客户端会发现服务器已经消失,但只有在断开超时之后,这是客户端尝试重新建立连接直到放弃的时间。 你可以change timeout value如果需要,但最好发送&#34;嘿,服务器正在下降&#34;给所有客户的信息......

相关问题