Sending a value from server to client with sockets

前端 未结 4 2114
情深已故
情深已故 2020-11-30 03:32

I am using the following projects in order to create an asynchronous communication between server and client sockets. When I am running those projects I am sending a message

4条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 03:51

    Why don't you make your life easier and use SignalR?
    Below you can see a simple example where the server and the clients are console apps

    Client (Run this .exe many times)

    using System;
    using Microsoft.AspNet.SignalR.Client;
    
    namespace SignalRClient
    {
        class Program
        {
            private static IHubProxy HubProxy { get; set; }
            const string ServerURI = "http://localhost:1234/signalr";
            private static HubConnection Connection { get; set; }
    
            static void Main(string[] args)
            {
                Connection = new HubConnection(ServerURI);
                HubProxy = Connection.CreateHubProxy("MyHub");
    
                HubProxy.On("SendMessage", (name, message) => Console.WriteLine(name + ":" + message));
                Connection.Start().Wait();
    
                Console.WriteLine("Press Enter to stop client");
                Console.ReadLine();
            }
        }
    }
    

    Server (Run this .exe before you run the clients)

    using System;
    using System.Threading.Tasks;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Client;
    using Microsoft.Owin.Hosting;
    using Owin;
    
    namespace SignalRServer
    {
        class Program
        {
            static private IDisposable SignalR { get; set; }
            const string ServerURI = "http://localhost:1234";
            private static IHubProxy HubProxy { get; set; }
            private static HubConnection Connection { get; set; }
            static void Main(string[] args)
            {
                SignalR = WebApp.Start(ServerURI);
                Console.WriteLine("Server running at " + ServerURI);
    
                Connection = new HubConnection(ServerURI);
                HubProxy = Connection.CreateHubProxy("MyHub");
                HubProxy.On("SendMessage", (name, message) => Console.WriteLine(name + ":" + message));
                Connection.Start().Wait();
    
                string messageToSentToClients;
                do
                {
                    Console.WriteLine("Type someting to send to clients and press enter");
                    messageToSentToClients = Console.ReadLine();
                   HubProxy.Invoke("Send", "Server", messageToSentToClients);
               } while (messageToSentToClients != "exit");
    
           }
        }
    
        public class MyHub : Hub
        {
            public void Send(string name, string message) { Clients.All.sendMessage(name, message); }
        }
    
        class Startup
        {
            public void Configuration(IAppBuilder app) { app.MapSignalR(); }
        }
    }
    

    In order for the above to work you need the following NuGet packages:

    Client:
    Microsoft.AspNet.SignalR.Client
    Server
    Microsoft.AspNet.SignalR
    Microsoft.AspNet.SignalR.Client
    Microsoft.AspNet.SignalR.SelfHost
    Microsoft.Owin.Host.HttpListener

    If you want the server/clients to be on different machines all you have to do is change the ServeURI properties in both projects:

    //Clients
    const string ServerURI = "http://SERVER_IP:PORT/signalr";
    //Server
    const string ServerURI = "http://SERVER_IP:PORT"; 
    

    You can find another similar example in WinForms here:
    https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b

提交回复
热议问题