后端代碼
10年積累的網(wǎng)站制作、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有唐河免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
push.php
?php
use?Workerman\Worker;
require_once?'./Workerman/Autoloader.php';
//?初始化一個(gè)worker容器,監(jiān)聽1234端口
$worker?=?new?Worker('websocket://0.0.0.0:1234');
//?這里進(jìn)程數(shù)必須設(shè)置為1
$worker-count?=?1;
//?worker進(jìn)程啟動(dòng)后建立一個(gè)內(nèi)部通訊端口
$worker-onWorkerStart?=?function($worker)
{
//?開啟一個(gè)內(nèi)部端口,方便內(nèi)部系統(tǒng)推送數(shù)據(jù),Text協(xié)議格式?文本+換行符
$inner_text_worker?=?new?Worker('Text://0.0.0.0:5678');
$inner_text_worker-onMessage?=?function($connection,?$buffer)
{
global?$worker;
//?$data數(shù)組格式,里面有uid,表示向那個(gè)uid的頁(yè)面推送數(shù)據(jù)
$data?=?json_decode($buffer,?true);
$uid?=?$data['uid'];
//?通過workerman,向uid的頁(yè)面推送數(shù)據(jù)
$ret?=?sendMessageByUid($uid,?$buffer);
//?返回推送結(jié)果
$connection-send($ret???'ok'?:?'fail');
};
$inner_text_worker-listen();
};
//?新增加一個(gè)屬性,用來保存uid到connection的映射
$worker-uidConnections?=?array();
//?當(dāng)有客戶端發(fā)來消息時(shí)執(zhí)行的回調(diào)函數(shù)
$worker-onMessage?=?function($connection,?$data)use($worker)
{
//?判斷當(dāng)前客戶端是否已經(jīng)驗(yàn)證,既是否設(shè)置了uid
if(!isset($connection-uid))
{
//?沒驗(yàn)證的話把第一個(gè)包當(dāng)做uid(這里為了方便演示,沒做真正的驗(yàn)證)
$connection-uid?=?$data;
/*?保存uid到connection的映射,這樣可以方便的通過uid查找connection,
*?實(shí)現(xiàn)針對(duì)特定uid推送數(shù)據(jù)
*/
$worker-uidConnections[$connection-uid]?=?$connection;
return;
}
};
//?當(dāng)有客戶端連接斷開時(shí)
$worker-onClose?=?function($connection)use($worker)
{
global?$worker;
if(isset($connection-uid))
{
//?連接斷開時(shí)刪除映射
unset($worker-uidConnections[$connection-uid]);
}
};
//?向所有驗(yàn)證的用戶推送數(shù)據(jù)
function?broadcast($message)
{
global?$worker;
foreach($worker-uidConnections?as?$connection)
{
$connection-send($message);
}
}
//?針對(duì)uid推送數(shù)據(jù)
function?sendMessageByUid($uid,?$message)
{
global?$worker;
if(isset($worker-uidConnections[$uid]))
{
$connection?=?$worker-uidConnections[$uid];
$connection-send($message);
return?true;
}
return?false;
}
//?運(yùn)行所有的worker(其實(shí)當(dāng)前只定義了一個(gè))
Worker::runAll();
啟動(dòng)后端服務(wù)
php?push.php?start?-d
前端接收推送的js代碼
var?ws?=?new?WebSocket('ws://127.0.0.1:1234');
ws.onopen?=?function(){
var?uid?=?'uid1';
ws.send(uid);
};
ws.onmessage?=?function(e){
alert(e.data);
};
后端推送消息的代碼
//?建立socket連接到內(nèi)部推送端口
$client?=?stream_socket_client('tcp://127.0.0.1:5678',?$errno,?$errmsg,?1,??STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);
//?推送的數(shù)據(jù),包含uid字段,表示是給這個(gè)uid推送
$data?=?array('uid'='uid1',?'percent'='88%');
//?發(fā)送數(shù)據(jù),注意5678端口是Text協(xié)議的端口,Text協(xié)議需要在數(shù)據(jù)末尾加上換行符
fwrite($client,?json_encode($data)."\n");
//?讀取推送結(jié)果
echo?fread($client,?8192);
這里的uid不一定是用戶的id,也可以理解為任務(wù)id即 taskid
以上代碼親測(cè)可以直接使用
首先引入數(shù)據(jù)庫(kù),然后用php代碼寫循環(huán)語(yǔ)句讀取鏈接寫入$urls
具體可以參考拾月素材里的帝國(guó)cms百度PHP主動(dòng)推送實(shí)例代碼詳解
PHP連接數(shù)據(jù)庫(kù)之PHP連接MYSQL數(shù)據(jù)庫(kù)代碼
?php???
$mysql_server_name='localhost';?
//改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器??
$mysql_username='root';?
//改成自己的mysql數(shù)據(jù)庫(kù)用戶名??
$mysql_password='12345678';?
//改成自己的mysql數(shù)據(jù)庫(kù)密碼??
$mysql_database='mycounter';
//改成自己的mysql數(shù)據(jù)庫(kù)名??
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);???
$sql='CREATE?DATABASE?mycounter?
DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;???
';???
mysql_query($sql);???
$sql='CREATE?TABLE?`counter`?
(`id`?INT(255)?UNSIGNED?NOT?NULL?
AUTO_INCREMENT?,`count`?INT(255)?
UNSIGNED?NOT?NULL?DEFAULT?0,PRIMARY?KEY?
(?`id`?)?)?TYPE?=?innodb;';???
mysql_select_db($mysql_database,$conn);???
$result=mysql_query($sql);???
//echo?$sql;???
mysql_close($conn);???
echo?"Hello!數(shù)據(jù)庫(kù)mycounter已經(jīng)成功建立!";???
??
PHP連接數(shù)據(jù)庫(kù)之PHP連接ACCESS數(shù)據(jù)庫(kù)代碼方法
???
$conn?=?new?com("ADODB.Connection");???
$connstr?=?"DRIVER={Microsoft
Access?Driver?(*.mdb)};?
DBQ=".?realpath("data/db.mdb");???
$conn-Open($connstr);???
$rs?=?new?com("ADODB.RecordSet");???
$rs-Open("select?*
from?szd_t",$conn,1,1);???
while(!?$rs-eof)?{???
$f?=?$rs-Fields(1);???
echo?$f-value;???
$rs-MoveNext();???
}???
?