C# Hprose轻量级、跨语言、跨平台的面向对象的高性能远程动态通讯中间件

3 篇文章 0 订阅

Hprose (High Performance Remote Object Service Engine) 是一个MIT开源许可的新型轻量级跨语言跨平台的面向对象的高性能远程动态通讯中间件。它支持众多语言,例如nodeJs, C++, .NET, Java, Delphi, Objective-C, ActionScript, JavaScript, ASP, PHP, Python, Ruby, Perl, Golang 等语言,通过 Hprose 可以在这些语言之间实现方便且高效的互通。
Hprose 易学易用,且功能强大,您只需很短时间的学习,就可以用它方便地构建出跨语言跨平台分布式的电信级应用系统。

国内码云git项目下载地址
https://gitee.com/andot/hprose-dotnet

github地址
https://github.com/hprose/hprose-dotnet

服务端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hprose.Server;

namespace HproseTcpServerTest {
    class Program {
        public static string Hello(string name) {
            return "Hello " + name + "!";
        }
        public static List<object> GetList(string word)
        {
            List<object> list = new List<object>();
            for (int i = 0; i < 100; i++)           
            {
                list.Add(word);
            }
            return list;
        }
        static void Main(string[] args) {
            try
            {
                HproseTcpListenerServer tcpserver = new HproseTcpListenerServer("tcp4://127.0.0.1:4321/");
                tcpserver.Add("Hello", typeof(Program));
                tcpserver.Add("GetList", typeof(Program));
                tcpserver.Start();


                HproseHttpListenerServer httpserver = new HproseHttpListenerServer("http://localhost:8888/");
                httpserver.Add("Hello", typeof(Program));
                httpserver.Add("GetList", typeof(Program));
                httpserver.Start();

                Console.WriteLine("服务已启动");
                Console.ReadKey();
                tcpserver.Stop();
                httpserver.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine("启动异常:"+ex.Message);
            }         
        }
    }
}

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hprose.Client;
using System.Diagnostics;
using System.Threading;

namespace HproseTcpClientTest
{
    public interface IHello
    {
        string Hello(string name);
        /// <summary>
        /// 得到集合
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        List<object> GetList(string word);
    }
    class Program
    {
        static IHello hello;

        static void Test(string data) {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++) {
                //hello.Hello(data);
                hello.GetList(data);
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
        }
        static void Main(string[] args)
        {

            HproseClient client = HproseClient.Create("tcp4://127.0.0.1:4321/");
            hello = client.UseService<IHello>();

            //20秒后执行          
            while (true)
            {
                if (DateTime.Now.Second == DateTime.Parse("2018-04-19 16:43:20").Second)
                {
                    break;
                }
                Thread.Sleep(500);
            }

            Console.WriteLine("TCP");
            //Console.WriteLine(hello.Hello("World"));
            Test("World");
            Test("".PadRight(512, '苏'));
            Test("".PadRight(1024, '苏'));
            Test("".PadRight(2 * 1024, '苏'));
            Test("".PadRight(4 * 1024, '苏'));
            Test("".PadRight(8 * 1024, '苏'));
            Test("".PadRight(16 * 1024, '苏'));
            Test("".PadRight(32 * 1024, '苏'));
            Test("".PadRight(64 * 1024, '苏'));
            Console.WriteLine("完成");

            client = HproseClient.Create("http://localhost:8888/");
            hello = client.UseService<IHello>();
            Console.WriteLine("HTTP");
            //Console.WriteLine(hello.Hello("World"));
            Test("World");
            Test("".PadRight(512, '苏'));
            Test("".PadRight(1024, '苏'));
            Test("".PadRight(2 * 1024, '苏'));
            Test("".PadRight(4 * 1024, '苏'));
            Test("".PadRight(8 * 1024, '苏'));
            Test("".PadRight(16 * 1024, '苏'));
            Test("".PadRight(32 * 1024, '苏'));
            Test("".PadRight(64 * 1024, '苏'));
            Console.WriteLine("完成");

            Console.ReadKey();

            //            client.Invoke<string>("hello", new Object[] { "Async World" }, result => Console.WriteLine(result));

        }
    }
}

demo下载地址
https://download.csdn.net/download/u011511086/10360956

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值