- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 PHP 和 Ratchet 库创建了一个 websocket,它在 WAMP 上本地运行,我在 AWS 上有一个运行 Ubuntu Debian 18.04 的服务器。
我正在尝试将我在本地创建的内容移动到服务器,但遇到两个错误。
第一个是在客户端上说:WebSocket connection to 'wss://localhost/socket' failed: WebSocket opening handshake timed out
第二个是在服务器上说:PHP Fatal error: Uncaught RuntimeException: Failed to listen on "tcp://0.0.0.0:443": Permission denied in /var/www/html/vendor/react/socket/src/TcpServer.php:165
我的客户端的简化版本是:
<input type="hidden" value="'.$account_id.'" id="account_id">
<script>
$(document).ready(function(){
var conn = new WebSocket('wss://localhost:443/socket');
conn.onopen = function(e){
console.log("Connection Opened!");
var account_id = $("#account_id").val();
var data = {account_id: account_id};
conn.send(JSON.stringify(data));
}
conn.onclose = function(e){
console.log("Connection Closed!");
}
conn.onmessage = function(e) {
var data = JSON.parse(e.data);
console.log(data);
};
conn.onerror = function(e){
var data = JSON.parse(e.data);
console.log(data);
}
})
</script>
我的服务器是这样的:
set_time_limit(0);
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
require dirname(__DIR__) . '/vendor/autoload.php';
class socket implements MessageComponentInterface{
protected $clients;
public function __construct(){
$this->clients = new \SplObjectStorage;
echo 'Server Started.'.PHP_EOL;
}
public function onOpen(ConnectionInterface $socket){
$this->clients->attach($socket);
echo 'New connection '.$socket->resourceId.'!'.PHP_EOL;
}
public function onClose(ConnectionInterface $socket) {
$this->clients->detach($socket);
echo 'Connection '.$socket->resourceId.' has disconnected'.PHP_EOL;
}
public function onError(ConnectionInterface $socket, \Exception $e) {
echo 'An error has occurred: '.$e->getMessage().'!'.PHP_EOL;
$socket->close();
}
public function onMessage(ConnectionInterface $from, $json){
echo 'Connection '.$from->resourceId.' sent '.$json.PHP_EOL;
$data = json_decode($json, true);
$account_id = $data['account_id'];
foreach ($this->clients as $client) {
if ($from == $client) {
$client->send(json_encode($data));
}
}
}
}
$server = IoServer::factory(
new HttpServer(
new WsServer(
new socket()
)
),
443
);
$server->run();
我更像是一名前端开发人员,所以我在网络和 apache 配置方面迷失了方向,所以这是我迄今为止检查过的内容:
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName dev.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) wss://localhost:443/$1 [P,L]
ProxyPass / https://localhost:443/
ProxyPassReverse / https://localhost:443/
</VirtualHost>
sudo netstat -ltpn
的输出
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 12993/mysqld
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 913/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1159/sshd
tcp6 0 0 :::80 :::* LISTEN 30191/apache2
tcp6 0 0 :::22 :::* LISTEN 1159/sshd
tcp6 0 0 :::443 :::* LISTEN 30191/apache2
编辑 2
/etc/apache2/sites-available/000-default.conf
而不是
/etc/apache2/sites-available/default-ssl.conf
.
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# A self-signed (snakeoil) certificate can be created by installing
# the ssl-cert package. See
# /usr/share/doc/apache2/README.Debian.gz for more info.
# If both key and certificate are stored in the same file, only the
# SSLCertificateFile directive is needed.
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
# Server Certificate Chain:
# Point SSLCertificateChainFile at a file containing the
# concatenation of PEM encoded CA certificates which form the
# certificate chain for the server certificate. Alternatively
# the referenced file can be the same as SSLCertificateFile
# when the CA certificates are directly appended to the server
# certificate for convinience.
#SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt
# Certificate Authority (CA):
# Set the CA certificate verification path where to find CA
# certificates for client authentication or alternatively one
# huge file containing all of them (file must be PEM encoded)
# Note: Inside SSLCACertificatePath you need hash symlinks
# to point to the certificate files. Use the provided
# Makefile to update the hash symlinks after changes.
#SSLCACertificatePath /etc/ssl/certs/
#SSLCACertificateFile /etc/apache2/ssl.crt/ca-bundle.crt
# Certificate Revocation Lists (CRL):
# Set the CA revocation path where to find CA CRLs for client
# authentication or alternatively one huge file containing all
# of them (file must be PEM encoded)
# Note: Inside SSLCARevocationPath you need hash symlinks
# to point to the certificate files. Use the provided
# Makefile to update the hash symlinks after changes.
#SSLCARevocationPath /etc/apache2/ssl.crl/
#SSLCARevocationFile /etc/apache2/ssl.crl/ca-bundle.crl
# Client Authentication (Type):
# Client certificate verification type and depth. Types are
# none, optional, require and optional_no_ca. Depth is a
# number which specifies how deeply to verify the certificate
# issuer chain before deciding the certificate is not valid.
#SSLVerifyClient require
#SSLVerifyDepth 10
# SSL Engine Options:
# Set various options for the SSL engine.
# o FakeBasicAuth:
# Translate the client X.509 into a Basic Authorisation. This means that
# the standard Auth/DBMAuth methods can be used for access control. The
# user name is the `one line' version of the client's X.509 certificate.
# Note that no password is obtained from the user. Every entry in the user
# file needs this password: `xxj31ZMTZzkVA'.
# o ExportCertData:
# This exports two additional environment variables: SSL_CLIENT_CERT and
# SSL_SERVER_CERT. These contain the PEM-encoded certificates of the
# server (always existing) and the client (only existing when client
# authentication is used). This can be used to import the certificates
# into CGI scripts.
# o StdEnvVars:
# This exports the standard SSL/TLS related `SSL_*' environment variables.
# Per default this exportation is switched off for performance reasons,
# because the extraction step is an expensive operation and is usually
# useless for serving static content. So one usually enables the
# exportation for CGI and SSI requests only.
# o OptRenegotiate:
# This enables optimized SSL connection renegotiation handling when SSL
# directives are used in per-directory context.
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
# SSL Protocol Adjustments:
# The safe and default but still SSL/TLS standard compliant shutdown
# approach is that mod_ssl sends the close notify alert but doesn't wait for
# the close notify alert from client. When you need a different shutdown
# approach you can use one of the following variables:
# o ssl-unclean-shutdown:
# This forces an unclean shutdown when the connection is closed, i.e. no
# SSL close notify alert is send or allowed to received. This violates
# the SSL/TLS standard but is needed for some brain-dead browsers. Use
# this when you receive I/O errors because of the standard approach where
# mod_ssl sends the close notify alert.
# o ssl-accurate-shutdown:
# This forces an accurate shutdown when the connection is closed, i.e. a
# SSL close notify alert is send and mod_ssl waits for the close notify
# alert of the client. This is 100% SSL/TLS standard compliant, but in
# practice often causes hanging connections with brain-dead browsers. Use
# this only for browsers where you know that their SSL implementation
# works correctly.
# Notice: Most problems of broken clients are also related to the HTTP
# keep-alive facility, so you usually additionally want to disable
# keep-alive for those clients, too. Use variable "nokeepalive" for this.
# Similarly, one has to force some clients to use HTTP/1.0 to workaround
# their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
# "force-response-1.0" for this.
# BrowserMatch "MSIE [2-6]" \
# nokeepalive ssl-unclean-shutdown \
# downgrade-1.0 force-response-1.0
</VirtualHost>
<VirtualHost *:80>
ServerName dev.demixer.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) wss://localhost:443/$1 [P,L]
</VirtualHost>
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
现在,当我启动服务器时,我没有收到任何错误,但在客户端上我看到:
WebSocket connection to 'wss://dev.example.com:8080/socket' failed: WebSocket opening handshake timed out
最佳答案
我在这里假设您正在尝试让 websocket 在 dev.demixer.com
上工作.虽然你试图用 dev.example.com
在别处掩盖它.我注意到您发布的第二个配置中的域名。 :)
第一部分
公开实际域帮助我弄清楚您正在使用让我们为 SSL 加密。我猜你用过 certbot
用于在 apache 上配置它?如果是这样,您仍然配置了错误的文件。该文件应该类似于 dev.demixer.com-le-ssl.conf
下 /etc/apache2/sites-available/
.该文件应具有如下配置。
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
ServerName dev.example.com
Include /etc/letsencrypt/options-ssl-apache.conf
#LogLevel warn rewrite:trace5
SSLCertificateFile /etc/letsencrypt/live/dev.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/dev.example.com/privkey.pem
## <-------------------- You need to add all your configuration in this section
</VirtualHost>
</IfModule>
第二部分
wss://
在客户端上,您不应该在
<VirtualHost *:80>
上添加您的配置部分。虽然在技术上可以指定 websocket url,如
wss://dev.example.com:80/socket
.这将是一种非标准做法,因为
HTTP
.所以,让我们使用端口
443
.
RewriteRule
除非你想要花哨的网址。对于更简单的套接字服务器配置,您可以一起跳过此配置。
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) wss://localhost:443/$1 [P,L]
回到您的
Ratchet
服务器配置它应该在apache不会使用的其他端口上启动。所以,端口不是
80
或
443
.
$server = IoServer::factory(
new HttpServer(
new WsServer(
new socket()
)
),
8080
);
Apache 应配置为
反向代理 用于在端口
8080
上运行的 Ratchet 服务器.最终配置应如下所示。
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
ServerName dev.example.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/dev.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/dev.example.com/privkey.pem
ProxyPreserveHost On
ProxyPass /socket/ ws://0.0.0.0:8080/
ProxyPassReverse /socket/ ws://0.0.0.0:8080/
</VirtualHost>
</IfModule>
而且,您的客户端套接字 url 将是
wss://dev.example.com/socket
解释
wss://dev.example.com/socket
---> Apache 端口 443
/socket
---> Rachet 在
8080
上运行
ws://
连接所有
SSL
图层将由
Apache
处理. Apache 的行为类似于
stunnel用于网络套接字
关于PHP/Ratchet websockets - 无法监听 "tcp://0.0.0.0:443",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65826832/
我需要一起实时收听 2 个网络套接字 URL。我创建了 2 个连接到不同 URL 的连接,但我只看到了第一条消息结果。如何在没有关闭第一次连接的情况下从 wss://exalpme2 获取消息? 我的
我正在尝试根据 YouTube 中的教程创建基本的 websocket 聊天,但在运行时我在终端中遇到了这个错误 php bin/server.php Fatal error: Interface '
我正在尝试在 Ratchet 中使用 sessionProvider,以下是我的 shell 脚本: namespace App\Console\Commands; use Ratchet\S
我正在尝试在我的系统上设置 Ratchet 并关注了 socketo.me 我已经做了一切,直到安装 ZMQ 和 React/Zmq 并且一切都成功了。 但是当我尝试运行时 push-server.p
我正在为我的应用程序使用 PHP Ratchet(推送集成),并具有实时出价功能,它的工作原理非常棒。 由于高流量,我们将我们的应用程序移到 AWS 平台上,我们有多个应用程序服务器实例,其中一个(R
我正在使用 Ratchet 为群聊启用推送通知。 我决定做以下事情: 每当用户连接时,为他订阅他参与的所有群组主题以及个人消息主题。 我有一个数组 protected $subscribedTopic
我只是在学习 Ratchet(用于 PHP 实时聊天应用程序的库)。根据文档,以下函数将收到两件事: 消息来自谁和 信息。 然而,它错过了发送给谁。此功能将消息发送给所有连接的人。但我也想实现一对一聊
我使用 Ratchet php 创建了一个网络套接字连接。我连接了一个客户端,然后执行了一个查询(执行查询大约需要 20 秒),同时我尝试连接另一个客户端,我看到网络套接字连接处于挂起状态(仍在尝试切
我正在开发一个具有聊天功能的网站,当用户收到新消息时需要即时通知。我正在尝试决定是使用 Ratchet 服务器还是使用 AJAX 实现长轮询系统。我目前已经实现了一个基本的 Pub/Sub Ratch
我正在尝试使 Ratchet 推送教程起作用。 http://socketo.me/docs/push 我正在按照教程所说的做,但我的订阅者不会收到任何消息。 我的服务器.php getSocket(
我开始使用 PhP Ratchet 套接字。按照指南,我可以制作一个简单的聊天应用程序,它可以在同一台计算机上运行。例如,如果我打开chrome和firefox,我可以交互发送和接收消息,ok。 问题
我是尝试实现 Ratchet 的编程初学者。 这是我当前的文件结构 D:\Xampp composer.phar htdocs Ratchet composer.
在 documentation , 他们有: Item 2 问:这只是打字错误,还是他们出于某种原因将 table-view-cell 放入了两次? 最佳答案 前几天我注意到了同样的事情并打算调查..
我是网络套接字编程的新手。我正在 codeigniter 中实现 web socket 以实现简单的聊天功能,我的配置看起来像这样 $config['ratchet_client'] = array
最近一直在学习如何使用 Ratchet 在 PHP 中实现 websockets,但我不知道如何让它与按钮点击一起工作。 这是我当前的 JS/jQuery 代码: jQuery(documen
我有一个托管在 AZURE 平台上的 Ubuntu 服务器。 SSH 的 IP 是 52.XX.XX.XX,如果我这样做了 ifconfig 表明 eth0 Link encap:Ethernet
我目前正在为 UI 使用 Ratchet 框架,并且想知道如何在页面之间共享信息(例如用户输入的数据)。该网站似乎没有涵盖这一点,我似乎无法找到任何类型的教程。 最佳答案 您最好的选择是使用 HTML
我已经使用 Ratchet 集成了 pusher。向所有用户广播工作正常。 现在我试图找到一种方法,当我连接到特定用户时如何向特定用户发送消息。 在 subscribe 上执行的方法: public
在客户端: var ws = new WebSocket('ws://localhost:8082/chat?id=123&tid=7'); 在服务器端 class MyApp implements
我使用 Ratchet 作为一些基于浏览器的游戏的套接字服务器,我注意到一个非常奇怪的行为。 我的应用程序类实现了 WampServerInterface,我注意到在 4-5 个客户端连接和断开连接(
我是一名优秀的程序员,十分优秀!