I have an own file that receives for post a request ajax, But the request never comes.
In a view:
<script type="text/javascript">
$(document).ready(function(){
$("#bloquear").click(function(e){
$.post("proyectodam/app/Lib/ServerSocket.php", {op: 1});
});
});
</script>
The php I have it in a directory that I have created,His namespaces:
namespace proyectodam\Lib;
file 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";
}
Client 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();
}
}
}
That fails?
If you are making a post request to proyectodam/app/Lib/ServerSocket.php
You need to make sure that you have a file in /public/proyectodam/app/Lib/ServerSocket.php
What I would suggest doing however is making an api endpoint such as:
Route::post('socket', function() {
return new ClientSocket();
}
And then have your client socket library class be:
<?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";
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.