在socket出現之前已經有ajax定時請求、長輪詢等方案,但都不能滿足需求,socket就應用而生了。
socket基本函數socket
總結下常用的socket函數
服務端: socket_create 創建socket設置基本參數
socket_bind 綁定ip和端口號
socket_listen 監聽
socket_accept 客戶端的連接
socket_read 讀取客戶端的數據
socket_write 給單獨客戶端發送數據
socket_close 關閉連接
客戶端:socket_create 創建socket設置基本參數
socket_connect 連接socket
socket_write 給服務端發送數據
socket_read 讀取服務端數據
socket_close 關閉連接
H5websocket不多說了,上鏈接
OK,開始貼代碼~
----------------------------------------------------------分割線
服務端代碼:
1 <?php
2 class WS {
3 var $master;
4 var $sockets = array();
5 var $debug = false;//true為調試模式,輸出log日志
6 var $handshake = array();
7
8 function __construct($address, $port){
9 $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
10 socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
11 socket_bind($this->master, $address, $port) or die("socket_bind() failed");
12 socket_listen($this->master,20) or die("socket_listen() failed");
13
14 $this->sockets[] = $this->master;
15 $this->say("Server Started : ".date('Y-m-d H:i:s'));
16 $this->say("Listening on : ".$address." port ".$port);
17 $this->say("Master socket : ".$this->master."\n");
18
19 while(true){
20 $socketArr = $this->sockets;
21 $write = NULL;
22 $except = NULL;
23 socket_select($socketArr, $write, $except, NULL); //自動選擇來消息的socket 如果是握手 自動選擇主機
24 foreach ($socketArr as $socket){
25 if ($socket == $this->master){ //主機
26 $client = socket_accept($this->master);
27 if ($client < 0){
28 $this->log("socket_accept() failed");
29 continue;
30 } else{
31 $this->connect($client);
32 }
33 } else {
34 $bytes = @socket_recv($socket,$buffer,2048,0);
35 if ($bytes == 0){
36 $this->disConnect($socket);
37 }
38 else{
39 $key = array_search($socket, $this->sockets);
40 if (empty($this->handshake) || !isset($this->handshake[$key]) || !$this->handshake[$key]){
41 $this->doHandShake($socket, $buffer, $key);
42 }
43 else{
44 $buffer = $this->decode($buffer);
45 echo $buffer.PHP_EOL;
46 $key = array_search($socket, $this->sockets);
47 $arr = $this->sockets;
48 array_shift($arr);
49 foreach ($arr as $s){
50 $this->send($s, $buffer);
51 }
52 }
53 }
54 }
55 }
56 }
57 }
58
59 function send($client, $msg){
60 $msg = $this->frame($msg);
61 socket_write($client, $msg, strlen($msg));
62 }
63 function connect($socket){
64 array_push($this->sockets, $socket);
65 $this->say("\n" . $socket . " CONNECTED!");
66 $this->say(date("Y-n-d H:i:s"));
67 }
68 function disConnect($socket){
69 $index = array_search($socket, $this->sockets);
70 socket_close($socket);
71 $this->say($socket . " DISCONNECTED!");
72 if ($index >= 0){
73 echo 'unset index is:'.PHP_EOL;
74 unset($this->sockets[$index]);
75 }
76 }
77 function doHandShake($socket, $buffer, $handKey){
78 $this->log("\nRequesting handshake...");
79 $this->log($buffer);
80 list($resource, $host, $origin, $key) = $this->getHeaders($buffer);
81 $this->log("Handshaking...");
82 $upgrade = "HTTP/1.1 101 Switching Protocol\r\n" .
83 "Upgrade: websocket\r\n" .
84 "Connection: Upgrade\r\n" .
85 "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n"; //必須以兩個回車結尾
86 $this->log($upgrade);
87 $sent = socket_write($socket, $upgrade, strlen($upgrade));
88 $this->handshake[$handKey]=true;
89 $this->log("Done handshaking...");
90 return true;
91 }
92
93 function getHeaders($req){
94 $r = $h = $o = $key = null;
95 if (preg_match("/GET (.*) HTTP/" ,$req,$match)) { $r = $match[1]; }
96 if (preg_match("/Host: (.*)\r\n/" ,$req,$match)) { $h = $match[1]; }
97 if (preg_match("/Origin: (.*)\r\n/" ,$req,$match)) { $o = $match[1]; }
98 if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)) { $key = $match[1]; }
99 return array($r, $h, $o, $key);
100 }
101
102 function calcKey($key){
103 //基於websocket version 13
104 $accept = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
105 return $accept;
106 }
107
108 function decode($buffer) {
109 $len = $masks = $data = $decoded = null;
110 $len = ord($buffer[1]) & 127;
111
112 if ($len === 126) {
113 $masks = substr($buffer, 4, 4);
114 $data = substr($buffer, 8);
115 }
116 else if ($len === 127) {
117 $masks = substr($buffer, 10, 4);
118 $data = substr($buffer, 14);
119 }
120 else {
121 $masks = substr($buffer, 2, 4);
122 $data = substr($buffer, 6);
123 }
124 for ($index = 0; $index < strlen($data); $index++) {
125 $decoded .= $data[$index] ^ $masks[$index % 4];
126 }
127 return $decoded;
128 }
129
130 function frame($s){
131 $a = str_split($s, 125);
132 if (count($a) == 1){
133 return "\x81" . chr(strlen($a[0])) . $a[0];
134 }
135 $ns = "";
136 foreach ($a as $o){
137 $ns .= "\x81" . chr(strlen($o)) . $o;
138 }
139 return $ns;
140 }
141
142
143 function say($msg = ""){
144 echo $msg . "\n";
145 }
146 function log($msg = ""){
147 if ($this->debug){
148 echo $msg . "\n";
149 }
150 }
151 }
152
153
154 new WS('localhost', 4000);
客戶端代碼(H5):
1 <html>
2 <head>
3 <title>demo</title>
4 <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
5 </head>
6 <body>
7 <input type="text" id="content">
8 <input type="button" value="send" id="send">
9 <script type="text/javascript">
10 var ws = new WebSocket("ws://localhost:4000");
11 ws.onopen = function(){
12 console.log("握手成功");
13 }
14 ws.onmessage = function(e){
15 console.log("message:" + e.data);
16 }
17 ws.onerror = function(){
18 console.log("error");
19 }
20 $("#send").click(function(){
21 content = $("#content").val();
22 console.log(content);
23 ws.send(content);
24 })
25 </script>
26 </body>
27 </html>
然后執行php demo.php 開啟socket(從運維那偷學一招,linux下執行nohup php demo.php &可以在后台執行),瀏覽器打開多個index.html,就能建立通訊了。
代碼解析:
1.屬性$sockets數組保存每個accept連接(不知道這么描述對不對);
2.屬性$handshake數組保存連接是否在連接狀態;

