前端连接了websocket 获取不到客户端的client_id, 请求 使用tp5 gateway-worker

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)  // 打印出来 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') {
            // 心跳检测 不做任何处理
        } else if (evt.type == 'chatGroup') {
            // 群组聊天
            $('.msgUl').append('<li>接收到:' + evt.data + '</li>')
        }
    };
    ws.onclose = function() {
        // console.log('连接已关闭...');
        $('.msgUl').append('<li>连接已关闭...</li>')
    };
    function sendValue() {
        $('#sendButton').click(function() {
            var thisValue = $('#sendValue').val();
            if (thisValue) {
                ws.send(thisValue);
                $('.msgUl').append('<li>发送数据:' + thisValue + '</li>')
                $.get("/index.php/index/Index/send_msg/msg/"+thisValue,function (data,status) {
                    console.log('成功:'+thisValue)
                });
            }
        })
    }
1255 1 0
1个回答

Tinywan

你需要贴出你 gateway-worker 怎么获取客户端IP的代码。而不是JS的代码。

gateway-worker 通常使用以下方法获取

$_SESSION['client_ip'] = $data['server']['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'];

更多观看:https://www.workerman.net/doc/gateway-worker/get-ip.html

年代过于久远,无法发表回答
🔝