如何将Android客户端连接到ReactPHP套接字服务器

时间:2019-04-23 07:12:09

标签: php android sockets client-server reactphp

我有与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客户端也需要它。 :)

1 个答案:

答案 0 :(得分:0)

我推荐这个图书馆Android Socket.io client