已经解决,这个贴子不能删除,请不用看了
workerman websocket 不能主动推送消息
nginx 配置
# WebSocket 代理配置
location /ws {
proxy_pass http://127.0.0.1:2345;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 86400s;
}
server 端代码
<?php
use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$ws_worker = new Worker('websocket://127.0.0.1:2345');
$ws_worker->count = 1;
$ws_worker->onWorkStart = function(Worker $worker) {
};
$ws_worker->onConnect = function(TcpConnection $connection) {
Timer::add(10, function() use($connection) {
$connection->send('hello world...');
});
};
$ws_worker->onMessage = function(TcpConnection $connection, $data) {
$connection->send('hello world '.time());
};
Worker::runAll();
html 代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>wss</title>
</head>
<body>
wss
<button id="j_btn" onclick="javascript:send();">xxxxx</button>
</body>
<script>
var ws = new WebSocket("wss://域名/ws");
ws.onopen = function() {
console.log("connect success.");
ws.send('tom');
console.log("send tom");
};
ws.onmessage = function(e) {
console.log("recv " + e.data);
};
function send() {
ws.send('tom2');
}
</script>
</html>
没有报错,就是在网页里收不到 server 主动推送的消息
onmessage 里的消息能收到,
onconnet 里定时发送的消息收不到