U3D 網路庫實現通訊 基於Warensoft Unity3d

Kaitiren發表於2015-09-10

如果使用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資料庫訪問

相關文章