標籤:其他 ajax 問題 reference receive 總結 dia wan href
前言問題
現在我們使用通用的應用程式和庫來進行通訊,例如:我們通常用一個HTTP 用戶端庫通過Web service調用遠程服務從一個web伺服器上擷取資訊。然而,一個通用的協議或其實現有時候不能很好地擴充。這就好比我們不使用HTTP 伺服器傳輸超大檔案,電子郵件資訊,和接近即時的資訊,比如財務資訊和多人遊戲資料。這些情境需要高度最佳化的協議實現。例如,你可能想要實現一個基於Ajax的聊天應用系統,流媒體,大型檔案傳輸的HTTP 伺服器。你甚至可能想設計和實現一種全新的協議能夠精確的滿足你的需求。另一種不可避免的情況就是,你不得不處理一個遺留專有協議來確保可以和老系統通訊。在這種情況下,重要的是,我們能夠多快的實現協議,而不會犧牲最終應用程式的穩定性和效能。
Nowadays we use general purpose applications or libraries to communicate with each other. For example, we often use an HTTP client library to retrieve information from a web server and to invoke a remote procedure call via web services. However, a general purpose protocol or its implementation sometimes does not scale very well. It is like how we don‘t use a general purpose HTTP server to exchange huge files, e-mail messages, and near-realtime messages such as financial information and multiplayer game data. What‘s required is a highly optimized protocol implementation which is dedicated to a special purpose. For example, you might want to implement an HTTP server which is optimized for AJAX-based chat application, media streaming, or large file transfer. You could even want to design and implement a whole new protocol which is precisely tailored to your need. Another inevitable case is when you have to deal with a legacy proprietary protocol to ensure the interoperability with an old system. What matters in this case is how quickly we can implement that protocol while not sacrificing the stability and performance of the resulting application.
解決方案
Netty 項目 是一個非同步事件驅動的網路應用程式架構,可以快速開發可維護的高效能,高擴充性協議伺服器和用戶端的工具。
換句話說,Netty是一個NIO用戶端伺服器架構,它能夠快速簡單的開發網路應用,比如協議伺服器和用戶端。它極大的簡化了網路編程,比如TCP和UDP的Socket伺服器開發。
“快速和簡單” 並不意味著應用程式會有維護性和效能的問題。Netty的設計仔細,通過從實現大量的協議,比如FTP,SMTP,HTTP和各種二級制和基於文本的遺留協議中獲得經驗,因此,Netty成功的找到了方法來同時實現了簡單開發,效能,穩定性和靈活性。
一些使用者可能也發現一些其他的架構聲稱有相同的優點,你可能會問什麼讓Netty如此與眾不同。答案就是我們打造的觀念。Netty從第一天起,就想給使用者最舒服的體驗,不管是API還是實現。它不是有形的東西但是你會意識到這種觀念,讓你的生活更簡單,就好像你讀這個指南和用Netty的時候。
The Netty project is an effort to provide an asynchronous event-driven network application framework and tooling for the rapid development of maintainable high-performance · high-scalability protocol servers and clients.
In other words, Netty is an NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server development.
‘Quick and easy‘ does not mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.
Some users might already have found other network application framework that claims to have the same advantage, and you might want to ask what makes Netty so different from them. The answer is the philosophy it is built on. Netty is designed to give you the most comfortable experience both in terms of the API and the implementation from the day one. It is not something tangible but you will realize that this philosophy will make your life much easier as you read this guide and play with Netty.
Netty Components Diagram
入門
本章將介紹Netty的核心架構,用一些簡單的例子讓你快速入門。當你結束本章的時候,你可以立刻寫一個基於Netty的用戶端伺服器程式。
如果你喜歡自上而下的學習東西,你也許想從第二章,架構介紹開始,然後在回到這裡。
This chapter tours around the core constructs of Netty with simple examples to let you get started quickly. You will be able to write a client and a server on top of Netty right away when you are at the end of this chapter.
If you prefer top-down approach in learning something, you might want to start from Chapter 2, Architectural Overview and get back here.
入門之前
本章中運行例子的最小需求只有兩個,最新的Netty版本和JDK 1.6以上。最新的Netty版本在 Netty下載頁 可用。為了下載正確版本的JDK,請參考JDK的供應商網站
正如你讀的那樣,你可能有很多問題關於這章中是用的class,當你想更多的知道它們的時候,請參考API reference。為了你的方便,所有的class都連結到了API reference上,而且,如果有任何錯誤的資訊,文法和拼字上的錯誤,或者你有好的建議提升我們的文檔,請立刻聯絡 Netty社區
The minimum requirements to run the examples which are introduced in this chapter are only two; the latest version of Netty and JDK 1.6 or above. The latest version of Netty is available in the project download page. To download the right version of JDK, please refer to your preferred JDK vendor‘s web site.
As you read, you might have more questions about the classes introduced in this chapter. Please refer to the API reference whenever you want to know more about them. All class names in this document are linked to the online API reference for your convenience. Also, please don‘t hesitate to contact the Netty project community and let us know if there‘s any incorrect information, errors in grammar and typo, and if you have a good idea to improve the documentation
樣本
下面的這些樣本你可以從netty安裝包的example jar解壓查看源碼
- Writing a Discard Server
- Looking into the Received Data
- Writing an Echo Server
- Writing a Time Server
- Writing a Time Client
- Dealing with a Stream-based Transport
- Speaking in POJO instead of
ByteBuf
- Shutting Down Your Application
總結
在這個章節,我們很快的學習了如何基於Netty寫一個完成的網路應用程式
在io.netty.example包中還有很多樣本可以查看
In this chapter, we had a quick tour of Netty with a demonstration on how to write a fully working network application on top of Netty.
There is more detailed information about Netty in the upcoming chapters. We also encourage you to review the Netty examples in the io.netty.example package.
Please also note that the community is always waiting for your questions and ideas to help you and keep improving Netty and its documentation based on your feedback.
Netty 4.x 使用者手冊