webman 安装gateway实现socket
首先进入webman目录
cd webman
执行以下命令
composer require webman/gateway-worker
配置业务
配置文件在 config/plugin/webman/gateway-worker/目录
业务目录在 plugin/webman/gateway 目录
设置config/plugin/webman/gateway-worker/目录下 app.php即可开启gatway-work插件
'enable' => true,
启动服务
php start.php start

文件设置 :config/plugin/webman/gateway-worker/process.php
<?php use Webman\GatewayWorker\Gateway; use Webman\GatewayWorker\BusinessWorker; use Webman\GatewayWorker\Register; return [ 'gateway' => [ 'handler' => Gateway::class, 'listen' => 'websocket://0.0.0.0:7272', 'count' => 2, 'reloadable' => false, 'constructor' => ['config' => [ 'lanIp' => '127.0.0.1', 'startPort' => 2300, 'pingInterval' => 25, 'pingData' => '{"type":"ping"}', 'registerAddress' => '127.0.0.1:1236', 'onConnect' => function(){}, ]] ], 'worker' => [ 'handler' => BusinessWorker::class, 'count' => cpu_count()*2, 'constructor' => ['config' => [ 'eventHandler' => plugin\webman\gateway\Events::class, 'name' => 'ChatBusinessWorker', 'registerAddress' => '127.0.0.1:1236', ]] ], 'register' => [ 'handler' => Register::class, 'listen' => 'text://127.0.0.1:1236', 'count' => 1, // Must be 1 'reloadable' => false, 'constructor' => [] ], ];
后的业务处理的文件:plugin/webman/gateway/Events.php
<?php namespace plugin\webman\gateway; use GatewayWorker\Lib\Gateway; class Events { public static function onWorkerStart($worker) { } public static function onConnect($client_id) { $data['type'] = 'get_client_id'; $data['data'] = ['client_id' => $client_id]; // 向当前client_id发送数据 Gateway::sendToClient($client_id, json_encode($data, JSON_UNESCAPED_UNICODE)); } public static function onWebSocketConnect($client_id, $data) { } public static function onMessage($client_id, $message) { Gateway::sendToClient($client_id, "receive message $message"); } public static function onClose($client_id) { } }
html文件
<script>
var wsUrl = 'ws://127.0.0.1:7272';
var ws = null; // WebSocket 对象
var heartbeatTimer = null; // 心跳定时器
var isReconnect = true; // 是否自动重连
function createWebSocket() {
if ("WebSocket" in window) {
ws = new WebSocket(wsUrl);
// WebSocket 打开事件
ws.onopen = function () {
console.log("WebSocket 已成功连接");
// 开始心跳定时器
startHeartbeat();
};
// WebSocket 收到消息事件
ws.onmessage = function (e) {
console.log("WebSocket 收到消息:" + e.data);
var data = e.data;
try {
JSON.parse(data);
data = JSON.parse(data) ? JSON.parse(data) : data;
} catch {
console.log('ws接收到非对象数据', data);
return true;
}
var type = data.type || '';
switch(type){
case 'get_client_id':
bindUid(data.data.client_id);
break;
case "new_customer":
var content = data.content;
window.top.notify.notice(content,'bottomRight');
var audio = $('#message_type_1').val();
if (audio != '') {
var audio_url = '../../../../assets/notice_mp3/'+audio;
var audio = new Audio();
audio.src = audio_url;
audio.play();
}
refreshSystemMessage();
break;
case "system_notice": //系统消息
if (audio != '') {
var audio_url = '../../../../assets/notice_mp3/system_notice.mp3';
var audio = new Audio();
audio.src = audio_url;
audio.play();
}
var content = data.content;
window.top.notify.notice(content,'bottomRight');
refreshSystemNotice();
break;
case "good_news":
var good_new_stop_second = data.good_new_stop_second; //停留时间
var template_key = data.system_background_image_index;
var preview_background_image = data.system_background_image;
var preview_photos_image = data.photos_image;
var good_new_object_str = data.good_new_object_str;
var good_new_content_str = data.good_new_content;
var url = "good_new/preview?key="+template_key+"&back_i="+preview_background_image+'&pho_i='+preview_photos_image+'&g_n_o_s='+good_new_object_str+'&g_n_c_s='+good_new_content_str+'&timeer='+good_new_stop_second;
var option = {
area:["1066px","576px"],
resize:false,
shadeClose:true,
shade:0.3,
time:good_new_stop_second * 1000,
closeBtn:0,
maxmin:false
};
window.top.Fast.api.open(url, '', option);
break;
}
};
// 发生错误回调
ws.onerror = function (e) {
// window.top.notify.warning("新客户提醒、一键拨号服务异常");
console.log('WebSocket错误:', e);
}
// WebSocket 关闭事件
ws.onclose = function () {
console.log("WebSocket 已关闭");
// 停止心跳定时器
stopHeartbeat();
// 断线后自动重连
if (isReconnect) {
setTimeout(function () {
console.log("WebSocket 尝试重新连接");
createWebSocket();
}, 3 * 1000);
}
};
} else {
console.log("该浏览器不支持 WebSocket");
}
}
// 发送消息
function sendMessage(message) {
if (ws != null && ws.readyState == WebSocket.OPEN) {
ws.send(message);
console.log("WebSocket 发送消息:" + message);
} else {
console.log("WebSocket 连接没有建立或已关闭");
}
}
// 开始心跳定时器
function startHeartbeat(interval) {
interval = interval || 30;
heartbeatTimer = setInterval(function () {
sendMessage("heartbeat");
}, interval * 1000);
}
// 停止心跳定时器
function stopHeartbeat() {
clearInterval(heartbeatTimer);
}
//绑定uid
function bindUid(client_id) {
console.log('WebSocket 绑定客户端id' + client_id);
var bindUrl = "{:url('socket/bindPcUserClientId')}";
$.post(bindUrl, {client_id: client_id}, function(data){
console.log(data);
}, 'json');
}
// 启动 WebSocket 连接
createWebSocket();
</script>
<style>
.notify-msg-info,.notify-msg-warning{
width: 300px;
background-color: #ffffff !important;
box-shadow: 0 0 2px 0 rgba(0, 1, 1, .01), 0 0 0 1px #e0e6ec !important;
padding: 15px !important;
}
.notify-msg-info .notify-msg-icon,.notify-msg-warning .notify-msg-icon{
display: none;
}
.message_title{
color: #333333;
font-size: 16px;
font-weight: 600;
}
.message_icon{
color: #15BAAA;
}
.message_content{
margin-top: 5px;
color: #333333;
font-size: 14px;
}
.notify-msg.notify-msg-info .notify-msg-wait,.notify-msg.notify-msg-warning .notify-msg-wait{
fill: #333333 !important;
}
.click_button{
margin-left: 10px;
color: #15BAAA;
cursor: pointer;
}
</style>
后端收到message,在onMessage进行处理
浙公网安备 33010602011771号