# 2. 客戶端與服務器端的交互
在我們測試和使用websocket之前,我們必須要有一個現成的websocket服務器,最好能夠快速搭建一個,所以現在必須快速搭建一個websocket服務器,在這里,分別介紹一個c++和go語言寫的websocket server的庫,還有,還會介紹如何用JavaScript來測試websocket server,方便我們了解整個websocket的工作流程。
#### 1. Simple-WebSocket-Server
[Simple-WebSocket-Server](https://github.com/eidheim/Simple-WebSocket-Server)是用c++語言寫的工具。只需幾行命令,就可以輕易地搭建websocket服務器。
```
git clone https://github.com/eidheim/Simple-WebSocket-Server
cd Simple-WebSocket-Server
cmake .
make
```
編譯完成之后會產生兩個可執行文件,其實是兩個demo程序,也是服務器端程序,分別是`ws_examples`和`wss_examples`,其中`wss_examples`是對應SSL加密的,需要證書,我們這里只需要`ws_examples`,先不管`wss_examples`。
運行`ws_examples`。
```
$ ./ws_examples
Server: Opened connection 140361834957120
Client: Opened connection
Client: Sending message: "Hello"
Server: Message received: "Hello" from 140361834957120
Server: Sending message "Hello" to 140361834957120
Client: Message received: "Hello"
Client: Sending close connection
Server: Closed connection 140361834957120 with status code 1000
Client: Closed connection with status code 1000
```
這個程序演示了websocket連接的整個過程,它先是在服務器端開啟websocket服務,然后客戶端連接上來,并發送"hello"給服務器端,服務器端接收后,又把"hello"這條信息發給客戶端,客戶端接收了"hello"之后,客戶端請求關閉連接,服務器端接收到客戶端發送的關閉連接的請求后,就關閉了連接,最后,客戶端也關閉了連接,整個流程就結束了。
#### 2. JavaScript訪問websocket服務
現在服務器也搭起來,要讓客戶端能夠連接上去,只需要使用JavaScript就可以了。也是幾行代碼就能搞定。
打開chrome瀏覽器的開發者工具,切換到`console`標簽。
輸入下面的指令。
```
new WebSocket("ws://localhost:8080/echo")
```
如下圖所示:

`Simple-WebSocket-Server`默認是監聽在8080端口的,且路徑是`/echo`。
有一個地方值得注意,你需要在localhost域名下訪問WebSocket服務。
現在來給WebSocket服務器發送一條字符串"hello"。
```
ws = new WebSocket("ws://localhost:8080/echo");
ws.onmessage = function(evt){console.log(evt.data);};
ws.onopen = function(evt){
ws.send("Hello");
}
```
其實就是利用`onmessage`和`onopen`這兩個回調函數,`ws.send("Hello");`表示向服務器發送"hello"。`onmessage`這個回調會把服務器的輸出用`console.log`打印出來。效果如下:

最后,客戶端要發送關閉連接的指令。
```
ws.close();
```
整個過程的代碼可以是這樣子的。
```
var ws;
window.onload = function(){
ws = new WebSocket("ws://localhost:8080/echo");
ws.onmessage = function(evt){console.log(evt.data);};
ws.onopen = function(evt){
ws.send("Hello");
}
}
window.onclose = function(){
ws.close();
}
```
#### 3. websocketd
> Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
[websocketd](https://github.com/joewalnes/websocketd)是一個用go語言寫的工具,它可以讓你的程序跑在websocket中。
我們來演示一下就清楚了。首先是安裝。
如果是mac系統,可以這樣。
```
$ brew install websocketd
```
如果是其他平臺,可以找到相應的安裝包,地址為: [https://github.com/joewalnes/websocketd/releases。](https://github.com/joewalnes/websocketd/releases%E3%80%82)
接下來我們創建一個文件,叫`count.sh`,內容如下:
```
#!/bin/bash
for ((COUNT = 1; COUNT <= 10; COUNT++)); do
echo $COUNT
sleep 1
done
```
這個shell腳本是每隔一秒就輸出一個數字,數字從1到10。
然后執行下面的指令,給這個文件加上可執行的權限。
```
$ chmod +x count.sh
```
最后運行這個websocket服務器。
```
$ websocketd --port=8080 ./count.sh
Thu, 07 Apr 2016 14:54:06 +0800 | INFO | server | | Serving using application : ./count.sh
Thu, 07 Apr 2016 14:54:06 +0800 | INFO | server | | Starting WebSocket server : ws://MacintoshdeMacBook-Air.local:8080/
```
測試方法跟上文所講的一樣,服務器仍然監聽在8080端口,只不過路徑是`/`。

本篇完結。
下一篇:[websocket之客戶端詳解(三)](http://www.rails365.net/articles/websocket-zhi-ke-hu-duan-xiang-jie-san)