Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# signalr console app client example

using System;
using SignalR.Client.Hubs;

namespace SignalRConsoleApp {
    internal class Program {
        private static void Main(string[] args) {
            //Set connection
            var connection = new HubConnection("http://127.0.0.1:8088/");
            //Make proxy to hub based on hub name on server
            var myHub = connection.CreateHubProxy("CustomHub");
            //Start connection

            connection.Start().ContinueWith(task => {
                if (task.IsFaulted) {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine("Connected");
                }

            }).Wait();

            myHub.Invoke<string>("Send", "HELLO World ").ContinueWith(task => {
                if (task.IsFaulted) {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                } else {
                    Console.WriteLine(task.Result);
                }
            });

            myHub.On<string>("addMessage", param => {
                Console.WriteLine(param);
            });

            myHub.Invoke<string>("DoSomething", "I'm doing something!!!").Wait();


            Console.Read();
            connection.Stop();
        }
    }
}
Comment

c# signalr console app server example

using System;
using SignalR.Hubs;

namespace SignalR.Hosting.Self.Samples {
    class Program {
        static void Main(string[] args) {
            string url = "http://127.0.0.1:8088/";
            var server = new Server(url);

            // Map the default hub url (/signalr)
            server.MapHubs();

            // Start the server
            server.Start();

            Console.WriteLine("Server running on {0}", url);

            // Keep going until somebody hits 'x'
            while (true) {
                ConsoleKeyInfo ki = Console.ReadKey(true);
                if (ki.Key == ConsoleKey.X) {
                    break;
                }
            }
        }

        [HubName("CustomHub")]
        public class MyHub : Hub {
            public string Send(string message) {
                return message;
            }

            public void DoSomething(string param) {
                Clients.addMessage(param);
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# check if char is string 
Csharp :: unity sort a list 
Csharp :: c# generate guid from hash 
Csharp :: c# how to check for internet connectivity 
Csharp :: if debug c# 
Csharp :: c# array max 
Csharp :: print all complete object in list c# 
Csharp :: save image in c# 
Csharp :: how to create public variable in c# 
Csharp :: c# string ends with 
Csharp :: c# get gridview DataKeyNames 
Csharp :: input unity 
Csharp :: how to get file type from base64 in c# 
Csharp :: unity rigid body variable 
Csharp :: c# implement ienumerable t 
Csharp :: how to evaluate code in c# 
Csharp :: parse strings into words C# 
Csharp :: unity get pivot position 
Csharp :: autofac .net core 6 
Csharp :: c# type of string 
Csharp :: pause unity game 
Csharp :: JsonConvert.DeserializeObject options camelcasing c# .net 
Csharp :: c# get witdh of matrix 
Csharp :: unity how to create a prefab 
Csharp :: vb.net check if datatable has rows 
Csharp :: SieveOfEratosthenes 
Csharp :: finding values in the registry 
Csharp :: how to decrease velocity of a Unity rigidbody 
Csharp :: how to mock http client c# 
Csharp :: c# .equals vs == 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =