简体   繁体   English

Ratchet:如何在同一服务器上同时使用聊天和推送通知

[英]Ratchet : How to use both chat and push notifications on the same server

i would like to use on the same server both applications "chat" and 'push notifications" comming from the tutorial of ratchet ( http://socketo.me ) - "Hello world" ( http://socketo.me/docs/hello-world ) - "push integration" with ZMQ .我想在同一台服务器上使用来自棘轮教程( http://socketo.me )-“Hello world”( http://socketo.me/docs/ )的应用程序“聊天”和“推送通知” hello-world ) - 与 ZMQ 的“推送集成”。
each application works well , i run chat-server.php (for chat) and push-server.php (for push integration) .每个应用程序都运行良好,我运行 chat-server.php(用于聊天)和 push-server.php(用于推送集成)。 But when i open two cmd window and run both it does not working .但是当我打开两个 cmd 窗口并同时运行它时,它不起作用。 It is maybe a stupid question , but i'm a beginner in this field .这可能是一个愚蠢的问题,但我是这个领域的初学者。

find below the code of both executable在两个可执行文件的代码下方找到


chat-server.php :聊天 server.php :

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

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

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

$server->run();

push-server.php :推送 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();

Thanks in advance for your help .在此先感谢您的帮助 。 let me know If you need more informations如果您需要更多信息,请告诉我

You can use separate files, chat-server.php and push-server.php , but you can't listen to the same port from two different files.您可以使用单独的文件chat-server.phppush-server.php ,但是您不能从两个不同的文件中侦听同一个端口。 You'll have to change one of them.你必须改变其中之一。 You can keep push-server.php 's port as 8080 and change chat-server.php 's port to 3000 (for example):您可以将push-server.php的端口保持为8080并将chat-server.php的端口更改为3000 (例如):

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

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

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

$server->run();

If you'd like to use a single server file, it's quite simple, all you have to do is add one line from chat-server.php to push-server.php (I wrote a comment next to it).如果你想使用单个服务器文件,这很简单,你所要做的就是从chat-server.php添加一行到push-server.php (我在它旁边写了一条评论)。 So your final file would look something like this:所以你的最终文件看起来像这样:

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();

Don't add anything else from server.php such as the use ...;不要从server.php添加任何其他内容,例如use ...; lines线

Make sure you have separate files /src/MyApp/Chat.php and /src/MyApp/Pusher.php with the classes Chat and Pusher respectively.确保您有单独的文件/src/MyApp/Chat.php/src/MyApp/Pusher.php 分别带有ChatPusher类。

Now when you run:现在当你运行时:

$ php push-server.php

It will perform the tasks that server.php did.它将执行server.php所做的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM