【问题标题】:Request ajax to file not control PHP in Laravel 5.1?在 Laravel 5.1 中请求 ajax 文件不控制 PHP?
【发布时间】:2017-01-05 20:30:14
【问题描述】:

我有一个自己的文件,用于接收发布请求 ajax,但请求永远不会到来。

观点:

<script type="text/javascript">
    $(document).ready(function(){
        $("#bloquear").click(function(e){
            $.post("proyectodam/app/Lib/ServerSocket.php", {op: 1});
        });
    });
</script>

我在我创建的目录中拥有它的php,他的命名空间:

namespace proyectodam\Lib;

文件 PHP:

    <?php 
namespace proyectodam\Lib;

try{
    set_time_limit(0);
    $address = '127.0.0.1';
    $port = 5555;
    // Create a TCP Stream socket
    $sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for  SQL_TCP
    // Bind the socket to an address/port
    socket_bind($sock, $address, $port) or die('Could not bind to address');  //0 for localhost
    // Start listening for connections
    socket_listen($sock);
    //loop and listen
    while(true){
      /* Accept incoming  requests and handle them as child processes */
      $client = socket_accept($sock);
      //$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);
      // Read the input  from the client – 1024000 bytes
      //$input = socket_read($client, 1024000);
      // from here you need to do your database stuff
      // and handle the response 

       // Display output  back to client
      if(isset($_POST["op"])){
          $message = 1;
          socket_write($client, $message, strlen($message));
      }
      socket_close($client);
    }
    // Close the master sockets
    socket_close($sock);
}catch(Exception $e){
    echo 'Excepción capturada: ',  $e->getMessage(), "\n";
}

客户端 java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClienteSocket{

    public static void main(String args[]){

        try {
            Socket client = new Socket("127.0.0.1", 5555);

            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream());

            String op;
            while((op = in.readLine()) != null){
                System.out.println(op);
            }

        }catch (IOException e){
            System.out.println("error");
            e.printStackTrace();
        }
    }
}

失败了?

【问题讨论】:

  • 您确定proyectodam/app/Lib/ServerSocket.php 位置/文件可以从浏览器访问吗??
  • 我认为你的问题并没有说太多,但为什么不直接将文件包含到你的路由控制器中呢?
  • 我的文件 php 是一个服务器套接字,它在命令行上运行,我需要从发送信息将他花费到 verver 并将这个发送到客户端 pd:我设法连接客户端与服务器
  • 我没有工作,我在java中有客户端的部分

标签: php ajax laravel post


【解决方案1】:

如果您正在向proyectodam/app/Lib/ServerSocket.php 发出帖子请求,您需要确保您在/public/proyectodam/app/Lib/ServerSocket.php 中有一个文件

我建议做的是制作一个 api 端点,例如:

Route::post('socket', function() {
    return new ClientSocket();
}

然后让您的客户端套接字库类成为:

<?php 
namespace proyectodam\Lib;

class ClienteSocket {

public function __construct() 
{
  try{
    set_time_limit(0);
    $address = '127.0.0.1';
    $port = 5555;

    // Create a TCP Stream socket
    $sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for  SQL_TCP

    // Bind the socket to an address/port
    socket_bind($sock, $address, $port) or die('Could not bind to address');  //0 for localhost

    // Start listening for connections
    socket_listen($sock);

    //loop and listen
    while(true){
        /* Accept incoming  requests and handle them as child processes */
        $client = socket_accept($sock);

        //$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);

        // Read the input  from the client – 1024000 bytes
        //$input = socket_read($client, 1024000);
        // from here you need to do your database stuff
        // and handle the response 

        // Display output  back to client
        if(isset($_POST["op"])){
            $message = 1;
            socket_write($client, $message, strlen($message));
        }
        socket_close($client);
    }

    // Close the master sockets
    socket_close($sock);
  } catch(Exception $e){
    echo 'Excepción capturada: ',  $e->getMessage(), "\n";
  }
}
}

【讨论】:

  • 不是错误,我没有收到请求ajax到服务器的数据
  • 如果您没有收到任何错误,那么问题出在哪里。听起来这可能不是 laravel 问题,而是您正在使用的实际脚本的问题。
  • 你放置客户端socket的地方是服务器socket,我的客户端是java
  • 我需要一个功能示例,我被这个问题逼得绝望:(
猜你喜欢
  • 2017-02-26
  • 2016-02-22
  • 1970-01-01
  • 2015-03-04
  • 2020-04-01
  • 1970-01-01
  • 2020-08-06
  • 2018-02-28
  • 1970-01-01
相关资源
最近更新 更多