Php SOCKET\u PUSH不使用ZMQContext发送任何内容?[被防火墙阻止…]

Php SOCKET\u PUSH不使用ZMQContext发送任何内容?[被防火墙阻止…],php,zeromq,ratchet,Php,Zeromq,Ratchet,我尝试按照本教程进行Ratchet/ZMQ套接字编程: 通过一点定制来了解更多信息 服务器本身运行良好,前端html之间的连接似乎正常。但是我无法找到向服务器发送消息的PHP文件 代码如下: SENDER.PHP $context = new ZMQContext(); $socket = $context->getSocket(ZMQ::SOCKET_PUSH,'my pusher'); $socket->connect('tcp://127.0.0.1:5555'

我尝试按照本教程进行Ratchet/ZMQ套接字编程:

通过一点定制来了解更多信息

服务器本身运行良好,前端html之间的连接似乎正常。但是我无法找到向服务器发送消息的PHP文件

代码如下:

SENDER.PHP

$context    = new ZMQContext();
$socket     = $context->getSocket(ZMQ::SOCKET_PUSH,'my pusher');
$socket->connect('tcp://127.0.0.1:5555');
$socket->send("SENDING A MESSAGE");
use Models\SCSTRealtimeSubsObject;

// The event loop that will keep on triggering
$loop   = React\EventLoop\Factory::create();

// Our custom pusher that will do the logic, $loop is optional
$pusher =  new SCSTRealtimeSubsObject;

// 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);
//Binding to itself means the client can only connect to itself
$pull->bind("tcp://127.0.0.1:5555");
//On a 'message' event, pass the data to the myMessageHandler method of the MyPusherClass
$pull->on('message', array($pusher, 'customAction'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // 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();
class SCSTRealtimeSubsObject implements WampServerInterface {

    public function __construct() {
        echo "Constructor call. \n";
    }
    public function customAction($msg){ // Message from the onMessage
        echo "There was a message: $msg";
    }
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    //                  WampServerInterface Implementations
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    public function onOpen(ConnectionInterface $conn) {
        echo "New connection! ({$conn->resourceId}) \n";
    }
    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        echo "Connection {$conn->resourceId} has disconnected \n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "There is an error ". $e->getMessage();
    }
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        echo "New subscriber : $topic \n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
        echo "Unsubscribed : $topic \n";
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        echo "Published $topic. \n";
        $conn->close();
    }
}
上面的代码就是我遇到的问题。当我在命令行中运行代码时

php sender.php

服务器至少应该显示一些反馈,但它没有给我任何信息。然后sender.php就退出了。 我一直在努力找出我错过了什么。至少前面的html位可以工作。 如何让sender.php发送消息?如有任何建议/建议/帮助,将不胜感激

以下是我的其他代码:

INDEX.html

当我从构造器获取消息时,此html文件正在连接

ab.debug(true,true);
var conn = new ab.Session('ws://localhost:8080',
        function() {
            conn.subscribe('kittensCategory', function(data) {
                // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
                console.log("New data available: ",data);
            });
        },
        function() {
            console.warn('WebSocket connection closed');
        },
        {'skipSubprotocolCheck': true}
);
SERVER.PHP

$context    = new ZMQContext();
$socket     = $context->getSocket(ZMQ::SOCKET_PUSH,'my pusher');
$socket->connect('tcp://127.0.0.1:5555');
$socket->send("SENDING A MESSAGE");
use Models\SCSTRealtimeSubsObject;

// The event loop that will keep on triggering
$loop   = React\EventLoop\Factory::create();

// Our custom pusher that will do the logic, $loop is optional
$pusher =  new SCSTRealtimeSubsObject;

// 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);
//Binding to itself means the client can only connect to itself
$pull->bind("tcp://127.0.0.1:5555");
//On a 'message' event, pass the data to the myMessageHandler method of the MyPusherClass
$pull->on('message', array($pusher, 'customAction'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // 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();
class SCSTRealtimeSubsObject implements WampServerInterface {

    public function __construct() {
        echo "Constructor call. \n";
    }
    public function customAction($msg){ // Message from the onMessage
        echo "There was a message: $msg";
    }
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    //                  WampServerInterface Implementations
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    public function onOpen(ConnectionInterface $conn) {
        echo "New connection! ({$conn->resourceId}) \n";
    }
    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        echo "Connection {$conn->resourceId} has disconnected \n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "There is an error ". $e->getMessage();
    }
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        echo "New subscriber : $topic \n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
        echo "Unsubscribed : $topic \n";
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        echo "Published $topic. \n";
        $conn->close();
    }
}
PUSHER.PHP

$context    = new ZMQContext();
$socket     = $context->getSocket(ZMQ::SOCKET_PUSH,'my pusher');
$socket->connect('tcp://127.0.0.1:5555');
$socket->send("SENDING A MESSAGE");
use Models\SCSTRealtimeSubsObject;

// The event loop that will keep on triggering
$loop   = React\EventLoop\Factory::create();

// Our custom pusher that will do the logic, $loop is optional
$pusher =  new SCSTRealtimeSubsObject;

// 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);
//Binding to itself means the client can only connect to itself
$pull->bind("tcp://127.0.0.1:5555");
//On a 'message' event, pass the data to the myMessageHandler method of the MyPusherClass
$pull->on('message', array($pusher, 'customAction'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // 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();
class SCSTRealtimeSubsObject implements WampServerInterface {

    public function __construct() {
        echo "Constructor call. \n";
    }
    public function customAction($msg){ // Message from the onMessage
        echo "There was a message: $msg";
    }
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    //                  WampServerInterface Implementations
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    public function onOpen(ConnectionInterface $conn) {
        echo "New connection! ({$conn->resourceId}) \n";
    }
    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        echo "Connection {$conn->resourceId} has disconnected \n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "There is an error ". $e->getMessage();
    }
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        echo "New subscriber : $topic \n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
        echo "Unsubscribed : $topic \n";
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        echo "Published $topic. \n";
        $conn->close();
    }
}

终于找到了答案。我忘了附上我的开发服务器在Windows上

在我的例子中,php cli由windows防火墙连接,并且被阻止访问任何网络和端口


要解决此问题,请转到控制面板->windows防火墙。查找入站CLI这很可能是PhP exe。允许访问网络,这样就可以了。

终于找到了答案。我忘了附上我的开发服务器在Windows上

在我的例子中,php cli由windows防火墙连接,并且被阻止访问任何网络和端口


要解决此问题,请转到控制面板->windows防火墙。查找入站CLI这很可能是PhP exe。允许访问网络,这样就可以了。

嘿,我的PHP很好用,但当我调用ajax执行文件时,它只是挂起并失败了。你也有同样的问题吗?我也在windows上。

嘿,我的PHP很好用,但当我调用ajax执行文件时,它只是挂起并失败。你也有同样的问题吗?我也在windows上。

你怎么知道(你在哪里证明过)它确实“没有使用
ZMQContext
发送任何东西?”你怎么知道(你在哪里证明过)它确实“没有使用
ZMQContext
发送任何东西?”