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

使用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 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题