U3D 网络库实现通信 基于Warensoft Unity3d

如果使用warensoft 无法实现的同学,建议使用u3d 的一个 http插件:  BestHttp(http / websocket)来实现;download url: http://download.csdn.net/detail/kaitiren/9101417


Warensoft Unity3d Communication Lib

this is a high performance communication library for Unity3d,including some easy-to-use httpclasses,andsocket classes. And especially,it brings a totally new method to access to MS SQL SERVER2005+via http protocol.

该类库是专门为Unity3D编写的一个高性能通信库,其中包括了若干十分易于使用的Http通信类以及Socket通信类.另外最特别之处在于,它引入了一全新的,基于Http协议的数据库访问组件,可以轻松访问MS SQL SERVER2005+.

Features

1. Microsoft C# naming standards

    微软命名规范

As a C# developer, you will find the that the unity3d naming standard is quite different, and not comfortable. But in this lib, everything (fields, properties, methods, events) you see will goes with Microsoft naming standards.

作为一个C#开发人员,您会发现Unity3d中的命名规范与其他的C#例程中的命名规范大不相同,如字段公有化等.但是在该类库中,所有能够看到的内容(包括字段,属性,方法,事件)全部符合微软命名规范.

2. Communication via Http protocol

    基于HTTP协议的通信

Every class which could be used to process http request and response in .net framework ,such as WebRequest, WebClient, will not work in unity3d, instead of them, the only class you can use is the WWW class. For the beginners, the usage of WWW class may seems strange(yeah, in a 3d engine, you need to do in that way), actually, it is totally different from the Microsoft way. And the most painful points are the memory leak when dispose the http resources, and  the multithreading concurrency problem(if you create a lot of instance of WWW class at the same time ,then some times the engine will throw an exception:Too Many Threads, and then the application crashes).

Unity3D,开发人员只能使用WWW类来处理Http的请求和响应,原有的在DotNet Framework中的WebRequest类和WebClient,Unity3D中是无法使用的.对于初学者来讲,WWW类的使用方法有点奇怪(当然,3D引擎中,你必须这样做), 事实上,WWW类的使用方式与微软的编程风格完全不同当然,最令人头疼的是当你释放WWW类所占用的内存资源时,会出现较为明显的内存泄漏另外,过多使用WWW类会产生多线程并发问题,当开发人员同时建立多个WWW类的实例来并发访问多个Web资源时,经常会出现Too Many Threads(线程太多)的异常,然后整个系统就崩溃了.

In Warensoft Unity Communication Lib, a totally new class UnityHttpClient will be the best alternative. the HttpClient class simplifies the process of getting response, and it controls the concurrency numbers automatically in the background. Just compare the two types of codes, the 1st type is implemented with WWW class ,and the 2nd type is implemented with HttpClient class:

Warensoft Unity3D通信库为您引入了一个全新代替方案:UnityHttpClient.使用UnityHttpClient类发送Http请求以及获取响应将变的极为简单,另外,该类在自动在后台控制并发的线程数量.请对比以下两段代码,第一段是使用WWW类实现的,第二段代码是使用UnityHttpClient类来实现的.

///

///With WWW Class

///

public class WWWTest:MonoBehaviour

{

    WWW www;

    void Start()

    {}

    private int initStep=0;

    void Update()

    {

        switch (this.initStep) {

        case 0:

            this.www=new WWW ("http://www.abc.com/default.aspx");

            this.initStep=1;

            break;

        case 1:

            if (this.WWW.isDone)//waite until the http response is finished

            {

                print(this.www.text);

            }

            break;

        default:

        break;

        }

    }

}

//With HttpClient class

public class HttpClientTest:MonoBehaviour

{

    UnityHttpClient client;

    void Start(){}

    private int initStep=0;

    void Update()

    {

if(this.initStep==0)

{

        //create an instance

         this.client=UnityCommunicationManager.CreateInstance().GetHttpClient();

        this.client.BeginGetHttpContent("http://www.abc.com/default.aspx",new Action<string>((result)=>

        {

            print(result);

        }));

        this.initStep=1;

         }

    }

}

//end of the test

As the code you see,it is not difficult to find that HOW EASY the usage of UnityHttpClient is!

正像你看到的一个,使用HttpClient类是如此的简单!

3. Communication via Tcp protocol

    基于TCP协议的通信

In most cases,http protocol would be your choice,but sometimes,you will need something faster, and real time push from the server to every client without client polling(duplex communication), for example,there are a lot of characters walking in the same scene, but they are not npcs,that means every character is controlled by someone in front of a client computer,so,you need a fast way to synchronize the positions of these person models.

使用Http方式进行远程通信,固然可以解决绝大多数问题,但是有些时候你可能需要更快的通信,并且需要服务器可以将实时数据直接推送到客户端(不需要客户端定时查询).例如,在同一个场景中可能有很多人物在走动,这些人物不是NPC,而且它们都是由计算机前面的人来控制的.因此,你需要一个更快速的方法去同步这些人模的坐标信息.

The most efficient way is to build a series of custom protocols which are based on Tcp protocol. But unfortunately,Unity3d dose not provide too much easy-to-read documents about the network view component. Or instead of network view component, you could choose the Socket class of .net framework,it will solve any problem of network communication, but it is also the lowest API,and hard to control.

最有效的解决方法就是基于TCP协议之上制定一系列的自定义协议.但不幸的是,关于Unity3D内置的network view组件,官方并没有提供太多的,易于阅读和理解的文档.或者,你可以使用.NET Framework中的Socket类取而代之.Socket类可以说是一个万能的通信类,没有它搞不定的,但同时Socket类也是最低层的一个类,并且十分难以控制.

Warensoft Unity Communication Lib brings an alternative,The SocketClient class, an easy-to-use and easy-to-control class

Warensoft Unity 通信库引入了一个用于替代的SocketClient,一个使用简单,控制极为容易的类.

4. Accessing to MS SQL SERVER2005 with Warensoft Data Server

通过Warensoft数据访问服务访问MS SQL SERVER2005+数据库

For security reasons,the unity3d web player could not access to MS SQL SERVER(ADO.NET is unavailable). According to the common security policy of RIA technologies(such as silverlight,flash,js),RIA clients doesn't have the right to access to databases. Under the web player circumstance,the best practice is Proxy Pattern or just expose a simple web service API (HTTP service, HTTP soap web service,ext) on the server.

出于安全角度考虑,Unity3DWebPlayer,是不可以访问MS SQL SERVER(ADO.NET不可用).SilverlightFlash一样,通常情况下富客户端应用一般都是不能访问数据库的(这是一点是默认的安全策略).WebPlayer的环境下,最佳的实践方式就是使用代理模式(Proxy Pattern),或者干脆就在Web服务器上提供一个简单的Web服务接口(可以基于HTTP方式的服务,也可以是一个SOAPWeb服务等).

So,in Warensoft Unity3d Communication Lib, we provide a set of client proxy classes for Warensoft data service. Just few steps, you will be able to access to a MS SQL SERVER database.

为此,我们为您提供了一个名为Warensoft数据服务的代理数据访问技术,并且在Warensoft Unity 通信库中提供了一组客户端代理类,仅仅需要几步,您就可以轻松的实现SQL SERVER数据库访问

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值