网站备案名称要求,合肥集团网站建设公司,aso优化推广公司,广州智能模板建站主要端末#xff1a;小程序 云服务器 客户端A 客户端B 客户端C
流程#xff1a;
1.小程序用户将文件通过小程序上传至云服务器#xff0c;同时发送一个websocket#xff0c;内部端口
2.云服务器与客户端ABC建立webscoket连接
3.当小程序用户需要上传到指定的客户端A电脑…主要端末小程序 云服务器 客户端A 客户端B 客户端C
流程
1.小程序用户将文件通过小程序上传至云服务器同时发送一个websocket内部端口
2.云服务器与客户端ABC建立webscoket连接
3.当小程序用户需要上传到指定的客户端A电脑上的时候我们通过webscoket只针对客户端A发送指令让客户端A去下载
4.下载完成后要么在客户端通过ajaxcurl更新数据库要么通过webscoket的回调函数curl更新数据库。
完毕
html代码
!DOCTYPE html
html langen
headmeta http-equivContent-Type contenttext/html; charsetutf-8 /titleTitle/title
/head
body
div idcontent/div
/body
script typetext/javascript src/static/jquery.min.js/script
scriptlet lockReconnect false; //避免ws重复连接let ws null; // 判断当前浏览器是否支持WebSocketlet wsUrl ws://0.0.0.125:1234;let printer_id createWebSocket(wsUrl); //连接wsfunction contentLog(msg){let pLength $(#content).find(p).length;if (pLength300){$(#content).html();}$(#content).append(pmsg/p)}function createWebSocket(url) {try {if (WebSocket in window) {ws new WebSocket(url);}initEventHandle();} catch (e) {reconnect(url);contentLog(e)}}function initEventHandle() {ws.onclose function() {reconnect(wsUrl);contentLog(printer_id文件连接关闭! new Date().toLocaleString())};ws.onerror function() {reconnect(wsUrl);contentLog(printer_id文件连接错误! new Date().toLocaleString())};ws.onopen function() {let msg {uid:printer_id};ws.send(JSON.stringify(msg));heartCheck.reset().start(); //心跳检测重置contentLog(printer_id文件连接成功! new Date().toLocaleString())};ws.onmessage function(event) { //如果获取到消息心跳检测重置heartCheck.reset().start(); //拿到任何消息都说明当前连接是正常的if (event.data ! pong) {contentLog(有下载文件! new Date().toLocaleString())let data JSON.parse(event.data);$.ajax({url:{:url(index/downloadFile)},data:data,type:post,success:function (ev){contentLog(ev)}});}};}// 监听窗口关闭事件当窗口关闭时主动去关闭websocket连接防止连接还没断开就关闭窗口server端会抛异常。window.onbeforeunload function() {ws.close();}function reconnect(url) {if (lockReconnect) return;lockReconnect true;setTimeout(function() { //没连接上会一直重连设置延迟避免请求过多createWebSocket(url);lockReconnect false;}, 2000);}//心跳检测let heartCheck {timeout: 60000, //60秒发一次心跳timeoutObj: null,serverTimeoutObj: null,reset: function() {clearTimeout(this.timeoutObj);clearTimeout(this.serverTimeoutObj);return this;},start: function() {let self this;this.timeoutObj setTimeout(function() {//这里发送一个心跳后端收到后返回一个心跳消息//onmessage拿到返回的心跳就说明连接正常ws.send(ping);contentLog(心跳测试);self.serverTimeoutObj setTimeout(function() { //如果超过一定时间还没重置说明后端主动断开了ws.close(); //如果onclose会执行reconnect我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次}, self.timeout)}, this.timeout)}}/script/htmlworkerman代码
?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . /vendor/autoload.php;// 初始化一个worker容器监听1234端口
$worker new Worker(websocket://0.0.0.0:1236);/** 注意这里进程数必须设置为1*/
$worker-count 1;
// worker进程启动后创建一个text Worker以便打开一个内部通讯端口
$worker-onWorkerStart function($worker)
{// 开启一个内部端口方便内部系统推送数据Text协议格式 文本换行符$inner_text_worker new Worker(text://127.0.0.1:5679);$inner_text_worker-onMessage function(TcpConnection $connection, $buffer){// $data数组格式里面有uid表示向那个uid的页面推送数据$data json_decode($buffer, true);$uid $data[uid];// 通过workerman向uid的页面推送数据
// $ret broadcast($buffer);$ret sendMessageByUid($uid, $buffer);// 返回推送结果$connection-send($ret ? ok : fail);};// ## 执行监听 ##$inner_text_worker-listen();
};
// 新增加一个属性用来保存uid到connection的映射
$worker-uidConnections array();
// 当有客户端发来消息时执行的回调函数
$worker-onMessage function(TcpConnection $connection, $data)
{global $worker;$post json_decode($data, true);if (isset($post[uid])) {// 判断当前客户端是否已经验证,既是否设置了uidif (!isset($worker-uidConnections[$post[uid]])) {// 没验证的话把第一个包当做uid这里为了方便演示没做真正的验证$connection-uid $post[uid];/* 保存uid到connection的映射这样可以方便的通过uid查找connection* 实现针对特定uid推送数据*/$worker-uidConnections[$connection-uid] $connection;return;}}else{echo $data;}
};// 当有客户端连接断开时
$worker-onClose function(TcpConnection $connection)
{global $worker;if(isset($connection-uid)){// 连接断开时删除映射unset($worker-uidConnections[$connection-uid]);}};// 向所有验证的用户推送数据
function broadcast($message)
{global $worker;foreach($worker-connections as $connection){$connection-send($message);}return true;
}// 针对uid推送数据
function sendMessageByUid($uid, $message)
{global $worker;if(isset($worker-uidConnections[$uid])){$connection $worker-uidConnections[$uid];$connection-send($message);return true;}return false;
}// 运行所有的worker
Worker::runAll();