我有与ReactPHP客户端通信的ReactPHP套接字服务器,但与android客户端也需要相同。我该怎么办,我有一些客户端Android应用程序,但无法连接。可以提供一些示例代码或一些解释的帮助。我是Java和android的新手。
套接字服务器-ReactPHP
<?php
require_once __DIR__ . '/../../../../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:3005', $loop);
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->write("Hello Vale " . $connection->getRemoteAddress() . "!\n");
$connection->write("Welcome to this amazing server!\n");
$connection->write("Here's a tip: don't say anything.\n");
$connection->on('data', function ($data) use ($connection) {
$connection->write("test", $data);
});
});
$loop->run();
客户端-ReactPHP
<?php
require_once __DIR__ . '/../../../../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);
$connector->connect('127.0.0.1:3005')->then(function (React\Socket\ConnectionInterface $connection) use ($loop) {
$connection->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));
$connection->write("Hello World!\n");
});
$loop->run();
这种通信效果很好,Android客户端也需要它。 :)