我想在现有的聊天中使用websockets,我有以下代码(来自socketo.me的示例):
推杆
class Pusher implements WampServerInterface
{
protected $subscribedTopics = [];
public function onSubscribe(ConnectionInterface $conn, $topic)
{
var_dump('on subscribe');die;
}
public function onPushEventData($event)
{
var_dump('push event');die;
}
}
WebSocket服务器
public function actionStartSocket()
{
$loop = \React\EventLoop\Factory::create();
$pusher = new Pusher();
$context = new \React\ZMQ\Context($loop);
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($pusher, 'onPushEventData'));
$webSock = new \React\Socket\Server('127.0.0.1:8081', $loop);
$webServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
}
我有一种将消息存储到数据库的方法。
数据来自Ajax的POST
请求,如果成功保存,则将消息发送到websocket,然后将保存的对象返回给客户端。
$message = $this->saveMessage($dialog, $currentUser, (string)$message);
$context = new \ZMQContext();
$socket = $context->getSocket(\ZMQ::SOCKET_PUSH);
$socket->connect("tcp://127.0.0.1:5555");
$socket->send(json_encode([
"status" => true
]));
return $message;
我无法将数据从服务器发送到Websocket。 我将Apache用作网络服务器,并认为virtualHost配置不正确。
Windows 10
Apache 2.4.33
和模块:
proxy
proxy_http
proxy_wstunnel
PHP 7.2.4
Ratchet(http://socketo.me) 有没有人遇到过类似的问题?
配置:
<VirtualHost *:80>
ServerName proj
DocumentRoot "c:/proj"
<Directory "c:/proj/">
Options +Indexes +Includes +FollowSymLinks
+MultiViews AllowOverride All Require local
</Directory>
RewriteEngine on
RewriteCond %{QUERY_STRING} transport=polling
RewriteRule /(.*)$ http://127.0.0.1:5555/$1 [P]
ProxyRequests off
ProxyPass / ws://127.0.0.1:5555
</VirtualHost>