安裝配置運行
1.項目的安裝
從http://code.google.com/p/cshbbrain/downloads/list 下載源代碼包,解壓。從Eclipse 導入項目。 或直接從Eclipse經過svn從http://cshbbrain.googlecode.com/svn/trunk/下載獲取項目代碼。在Eclipse編譯項目沒有錯誤提示即成功(項目編譯要求jdk1.6及以上版本)。
2.項目的配置
2.1項目系統參數配置
項目參數文件位置:src/com/jason/config.properties
項目系統參數列表:
#the port of the server
#服務器所使用的端口
port=9090
#the priority of the thread in pool
#服務器工做線程的優先級,使用默認優先級便可
maxPriority=5
#the tmp folder
#服務器使用的默認臨時文件夾
tmpRoot=e:/tmp/
#the factor of the work process thread per CPU kernel
#業務處理線程CPU內核因子,例如設置爲5則表示每一個CPU核將建立5個處理線程,若是你的服務器有4個內核,那麼將建立5*4=20個處理線程
requestWorker=5
#the factor of the read writer monitor thread per CPU kernel
#網絡數據讀寫監聽線程CPU內核因子,例如設置爲1則表示系統將爲每一個內核建立1個數據讀寫監聽線程,你能夠根據本身服務器的性能和須要配置2,4,8個讀寫監聽線程每內核;設置將更加靈活
monitorWorker=1
#the buffer size of the read buffer,unit is KB
#網絡數據讀取緩存大小,單位是KB
readBuffer=5
#the buffer size of the write buffer,unit is KB
#網絡數據發送緩存大小,單位是KB
writeBuffer=64
#the send buffer size of the system sockect
#系統相關的Sockect數據發送緩衝區大小,單位KB
sockectSendBufferSize=64
#the receive buffer of the system socket
#系統相關的Sockect數據接收緩衝區大小,單位KB
sockectReceiveBufferSize=5
#wheather the connection keep alive,1:keep alive,0:not keep alive
#設置服務器處理的鏈接是長鏈接,仍是端鏈接,websocket服務器默認爲長鏈接,不可更改
keepConnect=1
#the count of blank read
#設置服務器端空讀多少次以後放棄讀取客戶端的數據
zoreFetchCount = 10
#the min size and the max size of the buffer pool
#設置緩衝區池的最大值和最小值
minBufferPoolSize=1000
maxBufferPoolSize=1000
#whether open the broadcast thread
#設置系統是否開啓廣播線程
broadSwitch=0
2.2系統日誌配置
系統日誌文件位置:src/log4j.properties
日誌的配置請按照標準的log4j日誌進行配置便可
3.項目運行
找到src/com/jason/server/ws/Server.java文件,做爲application運行便可啓動程序。
服務器啓動成功後,默認會輸出以下日誌:
16:26:35,576 INFO MasterServer:232 - 數據讀取回寫監聽線程建立成功:請求數據傳輸監聽線程0
16:26:35,576 INFO MasterServer:467 - 請求處理調度線程建立完畢
16:26:35,592 INFO MasterServer:232 - 數據讀取回寫監聽線程建立成功:請求數據傳輸監聽線程1
16:26:35,592 INFO MasterServer:467 - 請求處理調度線程建立完畢
16:26:35,592 INFO MasterServer:169 - 消息廣播線程建立完畢
16:26:35,592 INFO MasterServer:527 - 鏈接監聽線程建立成功
16:26:35,607 INFO MasterServer:559 - 服務器準備就緒,等待請求到來
4.項目測試
找到項目下的test/chat_test.html,使用chrome打開,在服務器的後臺輸出你看到以下信息表示鏈接創建成功:
16:26:46,248 INFO WebSocketDecoder:111 - the msg received:
GET /echo HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 192.168.1.222:9090
Origin: null
Sec-WebSocket-Key: z1+1r6GtInP+11drO3r7Dg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
16:26:46,264 INFO WebSocketDecoder:233 - z1+1r6GtInP+11drO3r7Dg==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
16:26:46,279 INFO WebSocketDecoder:375 - x-webkit-deflate-frame
16:26:46,279 INFO WebSocketDecoder:454 - the response: HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 0FDvRkfYrdLsbNBFPe+SoM010x0=
Sec-WebSocket-Origin: null
Sec-WebSocket-Location: ws://192.168.1.222:9090/echo
在文本輸入框中隨便輸入一串字符abc,服務器返回Hello,abc.那麼恭喜你已經成功安裝配置好項目,你能夠試着在項目中添加你本身的業務了。