var ws = new WebSocket("ws://127.0.0.1:8282");
ws.onopen = function() {
$('.msgUl').append('<li>已連接上...</li>')
sendValue();
};
ws.onmessage = function(evt) {
console.log(evt) // 打印出來(lái) evt.client_id 為空
$('.msgUl').append('<li>接收到:' + evt.data + '</li>')
// 連接成功
$('.msgUl').append('<li>client_id:' + evt.client_id + '</li>')
var client = evt.data;
var split_client = client.split(" ");
if (evt.type == 'onConnect') {
$.ajax({
type: "post",
url: "/index.php/index/Index/user_bind",
data: {
client_id: split_client[0]
},
dataType: "json",
success: function (data) {
console.log('成功')
}
});
} else if (evt.type == 'ping') {
// 心跳檢測(cè) 不做任何處理
} else if (evt.type == 'chatGroup') {
// 群組聊天
$('.msgUl').append('<li>接收到:' + evt.data + '</li>')
}
};
ws.onclose = function() {
// console.log('連接已關(guān)閉...');
$('.msgUl').append('<li>連接已關(guān)閉...</li>')
};
function sendValue() {
$('#sendButton').click(function() {
var thisValue = $('#sendValue').val();
if (thisValue) {
ws.send(thisValue);
$('.msgUl').append('<li>發(fā)送數(shù)據(jù):' + thisValue + '</li>')
$.get("/index.php/index/Index/send_msg/msg/"+thisValue,function (data,status) {
console.log('成功:'+thisValue)
});
}
})
}
你需要貼出你 gateway-worker 怎么獲取客戶端IP的代碼。而不是JS的代碼。
gateway-worker 通常使用以下方法獲取
$_SESSION['client_ip'] = $data['server']['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'];