0

I've created the server in laravel command file & set in supervisor to run the socket server continuously to accept client msg

Laravel Command file code Server.php

$loop = React\EventLoop\Factory::create();
$IP = getHostByName(getHostName()); // this will get current server IP address // 192.168.0.50
$socket = new React\Socket\Server($IP.':8080', $loop);

$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->on('data', function ($data) use ($connection) {
        // process data sent from client
    });
});
$loop->run();

Client.php

$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);

$connector->connect('192.168.0.50:8080')->then(function (React\Socket\ConnectionInterface $connection) use ($loop,$data) {
    $connection->write($data); // sent data to Server.php
});
$loop->run();

This is working fine but when I check on the next day it will be sent data from Client.php but not received at Server.php Then restart Supervisor of Server.php / php artisan server then it working fine for the whole day

Kailas
  • 3,173
  • 5
  • 42
  • 52

1 Answers1

0

I've found what exactly happen.

I've set IP with port 192.168.0.50:8080 to communicate with the server. but checked the next day the IP is changed to 127.0.0.1:8080.

Below is the solution to communicate with any IP address. Server.php

$loop = React\EventLoop\Factory::create();
$IP = getHostByName(getHostName()); // this will get current server IP address // 192.168.0.50
$IP = '0.0.0.0'; -> set this to I/O to any IP address
$socket = new React\Socket\Server($IP.':8080', $loop);

$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->on('data', function ($data) use ($connection) {
        // process data sent from client
    });
});
$loop->run();

sudo lsof -i -P -n | grep LISTEN -> run this cmd to check 8080 port

with updated code, It's showing now *:8080

Kailas
  • 3,173
  • 5
  • 42
  • 52