【问题标题】:PHP Ratchet socket client for sending data用于发送数据的 PHP Ratchet 套接字客户端
【发布时间】:2017-01-13 18:22:11
【问题描述】:

我正在使用 PHP Ratchet Socket 服务器,我想通过 php 客户端将数据发送到此套接字服务器。我的套接字服务器与 HTML 5 Web 套接字配合良好,但 php 客户端无法正常工作。这是我的代码

$host    = "localhost";
$port    = 9000;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
// get server response
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  :".$result;
// close socket
socket_close($socket);

当我运行这段代码时,什么也没有发生...有什么帮助吗?

【问题讨论】:

    标签: php sockets ratchet


    【解决方案1】:

    您可能不是在寻找用于 websocket 的 PHP 客户端,而是在寻找推送服务器。 http://socketo.me/docs/push

    在 socketo.me 网站上,所有内容都解释了如何设置。 给你一个简短的总结:

    推送服务器是您的应用程序逻辑和 websocket 本身之间的层。 这将使您能够向推送服务器发送请求,然后将这些请求发送到消息所针对的 websocket 的客户端。

    如果您需要任何进一步的解释,请告诉我。

    【讨论】:

      【解决方案2】:

      这来晚了,但对于希望澄清这些概念的人可能会有所帮助。

      socket_create()socket_connect() 是 PHP 核心函数,可用于 TCPUDP 协议,但不适用于 webSocket 连接 (ws://uri:port )。

      对于这个问题的项目,最好使用@mitchken提到的现成的websocket客户端包(棘轮/棘爪,amphp/websocket-client等)。

      我个人使用过 amphp,它运行良好,特别是如果您需要更新特定用户/组的 UI 以响应后端发生的事件/通知,通过使用 PHP websocket 客户端发送消息并在 websocket 服务器适配器类的 onMessage() 事件中将消息重定向到目标用户。

      【讨论】:

        猜你喜欢
        • 2017-08-14
        • 2016-12-16
        • 1970-01-01
        • 2021-11-09
        • 2021-06-25
        • 1970-01-01
        • 2023-03-23
        • 2013-09-22
        • 1970-01-01
        相关资源
        最近更新 更多