doufubu2518 2018-10-09 17:45
浏览 1341

使用PHP发送到WebSocket服务器?

I'm running a Node.js WebSocket server on port 3000 on the same machine that I'm running Apache/PHP by running a reverse-proxy through Apache. This is my HTTPD file:

  <VirtualHost *:80>
     ProxyRequests off
     SSLProxyEngine on
     ProxyPass /node/ ws://localhost:3000/node/
     ProxyPassReverse /node/ ws://localhost:3000/node/
  </VirtualHost>
  <VirtualHost *:443>
     ProxyRequests off
     SSLProxyEngine on
     ProxyPass /node/ ws://localhost:3000/node/
     ProxyPassReverse /node/ ws://localhost:3000/node/
  </VirtualHost>

Let's say my domain name is "example.com". Then, to connect to the WebSocket server through a browser, I would use this JS:

var connection = new WebSocket("wss://example.com/node/");

This works fine, and I'm successfully sending and receiving messages from browsers.

However, I also need to be able to send (not receive) messages to the WebSocket server using PHP, from the same machine. I've tried several configurations of fsockopen, and none of them have worked. Here are the ones I've tried, with the resulting error messages:

$fp = fsockopen("example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("ssl://example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("tls://example.com/node/", 3000); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("http://example.com/node/", 3000); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?

$fp = fsockopen("https://example.com/node/", 3000); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?

$fp = fsockopen("wss://example.com/node/", 3000); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?

$fp = fsockopen("ws://example.com/node/", 3000); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?

$fp = fsockopen("example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("ssl://example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("tls://example.com/node/", 443); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("http://example.com/node/", 443); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?

$fp = fsockopen("https://example.com/node/", 443); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?

$fp = fsockopen("wss://example.com/node/", 443); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?

$fp = fsockopen("ws://example.com/node/", 443); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?

$fp = fsockopen("example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("ssl://example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("tls://example.com/node/", 80); //php_network_getaddresses: getaddrinfo failed: Name or service not known

$fp = fsockopen("http://example.com/node/", 80); //Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?

$fp = fsockopen("https://example.com/node/", 80); //Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?

$fp = fsockopen("wss://example.com/node/", 80); //Unable to find the socket transport "wss" - did you forget to enable it when you configured PHP?

$fp = fsockopen("ws://example.com/node/", 80); //Unable to find the socket transport "ws" - did you forget to enable it when you configured PHP?

I've also tried all of those configurations using "localhost" and "127.0.0.1". And I've also experimented using the built-in PHP library functions socket_create and socket_connect, with all of those same hostnames, IP's, and ports - all resulting in the same types of "unknown host" errors. What am I doing wrong? What is the right way to do this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP