SOCKET_PUSH 没有使用 ZMQContext 发送任何东西? [被防火墙阻止...]
SOCKET_PUSH not sending anything using ZMQContext? [Blocked by FireWall ...]
我正在尝试按照本教程进行 Ratchet/ZMQ 套接字编程:
http://socketo.me/docs/push
进行一些自定义以了解更多信息。
服务器本身 运行 正常,前端 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");
上面的代码是我遇到的问题。当我运行命令行中的代码
php sender.php
服务器至少应该显示一些反馈,但它没有给我任何信息。 sender.php 刚刚退出。
我一直在试图弄清楚我错过了什么。至少 front-html 位有效。
如何让 sender.php 发送消息?任何 suggestion/advise/help 将不胜感激。
下面是我的其余代码:
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
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();
PUSHER.PHP
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。允许访问网络,这应该可以解决问题。
我正在尝试按照本教程进行 Ratchet/ZMQ 套接字编程: http://socketo.me/docs/push
进行一些自定义以了解更多信息。
服务器本身 运行 正常,前端 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");
上面的代码是我遇到的问题。当我运行命令行中的代码
php sender.php
服务器至少应该显示一些反馈,但它没有给我任何信息。 sender.php 刚刚退出。 我一直在试图弄清楚我错过了什么。至少 front-html 位有效。 如何让 sender.php 发送消息?任何 suggestion/advise/help 将不胜感激。
下面是我的其余代码:
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
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();
PUSHER.PHP
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。允许访问网络,这应该可以解决问题。