gpt4 book ai didi

php - 棘轮 : How to use both chat and push notifications on the same server

转载 作者:行者123 更新时间:2023-12-03 21:38:32 25 4
gpt4 key购买 nike

我想在同一台服务器上使用来自棘轮教程的应用程序“聊天”和“推送通知”(http://socketo.me)
- “ Hello World ”(http://socketo.me/docs/hello-world)
- 与 ZMQ 的“推送集成”。
每个应用程序都运行良好,我运行 chat-server.php(用于聊天)和 push-server.php(用于推送集成)。但是当我打开两个 cmd 窗口并同时运行它时,它不起作用。这可能是一个愚蠢的问题,但我是这个领域的初学者。

在两个可执行文件的代码下方找到

聊天 server.php :

使用 Ratchet\Server\IoServer;
使用 MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = IoServer::factory(
new Chat(),
8080
);

$server->run();

推送 server.php :
require dirname(__DIR__) . '/vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);

$loop->run();

在此先感谢您的帮助 。如果您需要更多信息,请告诉我

最佳答案

您可以使用单独的文件,聊天服务器.php push-server.php ,但是您不能从两个不同的文件中监听同一个端口。你必须改变其中之一。您可以保留 push-server.php 的端口为 8080并更改 聊天服务器.php 的端口到 3000 (例如):

use Ratchet\Server\IoServer; use MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = IoServer::factory(
new Chat(),
3000
);

$server->run();

如果您想使用单个服务器文件,这很简单,您只需添加来自 的一行即可。聊天服务器.php push-server.php (我在旁边写了一条评论)。所以你的最终文件看起来像这样:
require dirname(__DIR__) . '/vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new MyApp\Chat(), // This is the line added
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);

$loop->run();
不要添加来自 的任何其他内容server.php use ...;线
确保您有单独的文件 /src/MyApp/Chat.php /src/MyApp/Pusher.php 与类(class) ChatPusher分别。
现在当你运行时:
$ php push-server.php
它将执行 的任务server.php 做过。

关于php - 棘轮 : How to use both chat and push notifications on the same server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31021981/

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