gpt4 book ai didi

php - 我无法与 ReactPHP 的 Pawl 和 Ratchet 交叉通信

转载 作者:行者123 更新时间:2023-12-04 16:13:41 32 4
gpt4 key购买 nike

我目前正在尝试连接到两个不同的套接字服务器。其中一个本质上是一个 IRC 连接,另一个是我自己制作的接口(interface)服务器。这两个循环需要能够相互通信,但实际上我很难从一个连接向另一个连接发送消息。

这是我一直在尝试作为注入(inject)消息的简化方式,评论不是很自信,因为老实说我不确定我哪里出错了:

<?php

require __DIR__ . '/vendor/autoload.php';

// EventLoop the way I understand it is designed to split up your loops and
// run them one after another so you can kind of multithread, even though
// it's just one step of the loop at a time.
$loop = \React\EventLoop\Factory::create();

// Verbose defintion of connectors mainly trying to just gain extra control
// and so I could see how each thing was defined.
$reactConnector = new \React\Socket\Connector($loop, [
'dns' => '8.8.8.8',
'timeout' => 10
]);

$connector = new \Ratchet\Client\Connector($loop, $reactConnector);

// Connect as a client to the development Socket server. This is all just
// from the Pawl github essentially.
//
// The connection is successful every time.
$ws = $connector('ws://0.0.0.0:5069', [], ['Origin' => 'http://localhost']);
->then(function(Ratchet\Client\WebSocket $conn) {

// Simple echo on message received for the test.
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
echo "Received: {$msg}\n";
});

$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});

$conn->send('Hello World!');
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});

// Instead of including a second socket connector I decided to use a simple
// timer and try to get it to use the client connection above to send
// messages to the socket server.
//
// The problem is here, I can't get the socket server to send a message
// from outside of the ->then();
$loop->addPeriodicTimer(1, function () use ($ws) {

$ws->then(function (Ratchet\Client\WebSocket $conn) {
$conn->send('Figured out?');
});

});

$loop->run();

我真的很希望能够通过某种 $ws->send('message'); 从一个连接到另一个连接发送消息,但我终生无法弄清楚如何。

最佳答案

啊啊啊终于可以回答的问题了!!我昨天大部分时间都在处理我自己的 Ratchet/Pawl 客户端,这些客户端必须具有 addPeriodicTimer 循环才能定期发送内容。这花了一些时间,但我通过将 $loop->addPeriodicTimer() 调用放置在连接器块的内部,在 ->then(function(Ratchet\Client\WebSocket $conn) 部分之后和 $conn- 之前使其工作>on('message'...) 调用。同样对于 $loop->addPeriodicTimer 调用,确保添加 使用 子句传入连接......并确保添加 使用 子句将 $loop 传递给连接器。

<?php

require __DIR__ . '/vendor/autoload.php';

// EventLoop the way I understand it is designed to split up your loops and
// run them one after another so you can kind of multithread, even though
// it's just one step of the loop at a time.
$loop = \React\EventLoop\Factory::create();

// Verbose defintion of connectors mainly trying to just gain extra control
// and so I could see how each thing was defined.
$reactConnector = new \React\Socket\Connector($loop, [
'dns' => '8.8.8.8',
'timeout' => 10
]);

$connector = new \Ratchet\Client\Connector($loop, $reactConnector);

// Connect as a client to the development Socket server. This is all just
// from the Pawl github essentially.
//
// The connection is successful every time.
$ws = $connector('ws://0.0.0.0:5069', [], ['Origin' => 'http://localhost']);
->then(function(Ratchet\Client\WebSocket $conn) use ( $loop ) {


$loop->addPeriodicTimer(1, function () use ( $conn ) {

$conn->send('Figured out?');

});

// Simple echo on message received for the test.
$conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
echo "Received: {$msg}\n";
});

$conn->on('close', function($code = null, $reason = null) {
echo "Connection closed ({$code} - {$reason})\n";
});

$conn->send('Hello World!');
}, function(\Exception $e) use ($loop) {
echo "Could not connect: {$e->getMessage()}\n";
$loop->stop();
});


$loop->run();

关于php - 我无法与 ReactPHP 的 Pawl 和 Ratchet 交叉通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55860892/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com