gpt4 book ai didi

php - SOCKET_PUSH 不使用 ZMQContext 发送任何内容? [被防火墙阻止...]

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

我正在尝试按照本教程进行 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 发送消息?任何建议/建议/帮助将不胜感激。

以下是我的其余代码:

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。允许访问网络,这应该可以解决问题。

关于php - SOCKET_PUSH 不使用 ZMQContext 发送任何内容? [被防火墙阻止...],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49643365/

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