TCP Client 請自行根據作業系統以及個人喜好選擇,記得通訊字串最後要「換行」
--
Socket1.php - 只會顯示接收訊息的 Socket Server
這是第一個功能最簡單的 Socket 程式,只會把接收的訊息顯示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman/Autoloader.php'; use Workerman\Worker; $w = new Worker("text://0.0.0.0:3101"); $w->count = 1; // 接收時執行 $w->onMessage = function($connection, $data) { echo $data."\n"; }; // 运行 worker Worker::runAll(); |
執行起來像這樣
--
Socket2.php - 將接收訊息複製回傳並加上流水號
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman/Autoloader.php'; use Workerman\Worker; $w = new Worker("text://0.0.0.0:3101"); $w->count = 1; $i = 1; // 接收時執行 $w->onMessage = function($connection, $data) { global $i; $connection->send($i .'. '. $data."\n"); $i++; }; // 运行 worker Worker::runAll(); |
--
Socket3.php - 儲存資料到資料庫
常駐程式內要使用資料庫,要注意 timeout 問題,Hoyo 目前的解決方法是使用短連結 PDO::ATTR_PERSISTENT=>false ,以及不使用 global 傳遞資源,每個 funtion(){} 內建立 PDO 資源,如此每個 MySQL 連線都是新建立,沒有長連結共用問題就沒有 MySQL server has gone away 錯誤
Socket3.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman/Autoloader.php'; use Workerman\Worker; $w = new Worker("text://0.0.0.0:3101"); $w->count = 1; $i = 1; // 資料庫 const MySQL_Host = "127.0.0.1"; const MySQL_Username = "username"; const MySQL_Password = "password"; const MySQL_Database = "database"; // $w->onMessage = function($connection, $data) { $db = new PDO(MySQL, MySQL_Username, MySQL_Password, array( PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' )); // $sql = "INSERT INTO `Table` (a,b) VALUES ('1','c') "; $pre = $db->prepare($sql); $pre->execute(); }; // 运行 worker Worker::runAll(); |
--
Socket4.php - 紀錄 Client 以及推送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman_3.5.22/Autoloader.php'; use Workerman\Worker; $w = new Worker("text://0.0.0.0:3101"); $w->count = 1; $w->clients = array(); $i = 1; $w->onMessage = function($connection, $data)use($w) { $connection->uid = $data; $w->clients[$connection->uid] = $connection; $w->clients['a']->send('ok!'); }; // 运行 worker Worker::runAll(); |
--
Socket5.php - 不須 restart 更新程式方式
只須將程式片段另外儲存 php 後,使用 require 引入使用即可,不過修改對象是 Socket5.php 還是需要 stop 再 start 才行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman/Autoloader.php'; use Workerman\Worker; $w = new Worker("text://0.0.0.0:3101"); $w->count = 1; $i = 1; // 資料庫 const MySQL_Host = "127.0.0.1"; const MySQL_Username = "username"; const MySQL_Password = "password"; const MySQL_Database = "database"; // $w->onMessage = function($connection, $data) { $db = new PDO(MySQL, MySQL_Username, MySQL_Password, array( PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' )); // require 'mysql.php'; }; // 运行 worker Worker::runAll(); |
mysql.php
1 2 3 4 |
<?php $sql = "INSERT INTO `Table` (a,b) VALUES ('1','c') "; $pre = $db->prepare($sql); $pre->execute(); |
--
Socket6.php - SSL 通訊加密
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php date_default_timezone_set('Asia/Taipei'); require_once './Workerman_3.5.22/Autoloader.php'; use Workerman\Worker; // 证书最好是申请的证书 $context = array( 'ssl' => array( 'local_cert' => 'etc/httpd/ssl/apache.crt', // 也可以是crt文件 'local_pk' => '/etc/httpd/ssl/apache.key', 'verify_peer' => false, ) ); $w = new Worker("text://0.0.0.0:3101", $context); // 设置transport开启ssl $w->transport = 'ssl'; $w->count = 1; $i = 1; $w->onMessage = function($connection, $data)use($w) { $connection->send('ok!'); }; // 运行 worker Worker::runAll(); |
如果是 Python 這類較高階程式可以複製 apache.crt 就可以建立 SSL 加密通訊,ESP8266 目前是使用指紋憑證,可以使用 How to View a Certificate Fingerprint as SHA-256, SHA-1 or MD5 using OpenSSL 的查詢指令
1 2 |
# openssl x509 -noout -fingerprint -sha1 -inform pem -in /etc/httpd/ssl/apache.crt SHA1 Fingerprint=F8:7A:40:BF:90:7F:5E:7D:B6:2D:F6:E5:BB:F8:30:55:F0:BC:6B:42 |
--
5,031 total views, 1 views today