Witam
Mam pewien problem z socketem.
Używam go do odbioru paczki danych (kilkanaście bajtów). Dane są wysyłane co 30 sekund.
Do symulacji użyłem skryptu w node.
Problem jest taki że w momencie kiedy uruchomię skrypt wszystko działa ale kiedy prześlę dane z urządzenia (dane GPS) skrypt PHP odbierze je tylko raz i koniec.
Jeżeli uruchomię klienta + tracker GPS wszystko też działa (pomiędzy danymi ze skryptu otrzymuje dane z Trackera.
Próbowałem też z socketem pod Node ale nie działa skrypt clienta wywala exception więc próbuję pod php ale na razie z marnym skutkiem.
<?php
//////////////////serwer////////////////////////////
echo "OK";
/*************************************/
/********Socket Server*********************/
set_time_limit (0);
// Set the ip and port we will listen on
$address = 'xxxxxx';
$port = 4567;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address'); //0
socket_listen($sock);
while (true) {
$client = socket_accept($sock);
$input = socket_read($client, 1000); // ????
// Strip all white spaces from input
echo $input;
flush();
ob_flush();
sleep(1);
// Close the master sockets
}
socket_close($sock);
?>
//////////////////////klient/////////////////////////
var net = require('net');
function loop(){
var net = require('net');
var HOST = 'thx.atthost24.pl';
var PORT = 4567;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('Client connected to: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('Hello World!');
});
client.on('data', function(data) {
console.log('Client received: ' + data);
if (data.toString().endsWith('exit')) {
client.destroy();
}
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Client closed');
});
client.on('error', function(err) {
console.error(err);
});
}
setInterval(loop,3000);