服務(wù)端代碼是這么寫的
<?php
include __DIR__ . '/workerman/Autoloader.php';
use Workerman\Worker;
// 這里設(shè)置的是websocket協(xié)議(端口任意,但是需要保證沒(méi)被其它程序占用)
$worker = new Worker('websocket://0.0.0.0:1010');
// 設(shè)置transport開啟ssl,websocket+ssl即wss
//$worker->transport = 'ssl';
$worker->onMessage = function($connection, $data)
{
$connection->send("hello");
};
Worker::runAll();
下面是 nginx的配置文件
server {
listen 80;
server_name xxxxxxxxxxxxxx;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name xxxxxxxxxxxxxx;
index index.html index.htm index.php;
ssl on;
ssl_certificate /data/home/server/nginx-1.7/conf/ssl/xxxxxxxxxxxxxx/xxxxxxxxxxxxxx.pem;
ssl_certificate_key /data/home/server/nginx-1.7/conf/ssl/xxxxxxxxxxxxxx/xxxxxxxxxxxxxx.key;
ssl_session_timeout 5m;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:50m;
ssl_prefer_server_ciphers on;
location / {
root /data/home/www/xxxxxxxxxxxxxx/public;
index index.php index.html index.htm;
# Nginx環(huán)境配置,隱藏ThinkPHP public index.php入口
#try_files $uri $uri/ /index.php?s=$uri&$args;
try_files $uri $uri/ /index.php?s=$args;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location /wss
{
proxy_pass http://127.0.0.1:8686;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
location ~ \.php { #去掉$
root /data/home/www/xxxxxxxxxxxxxx/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$; #增加這一句
fastcgi_param PATH_INFO $fastcgi_path_info; #增加這一句
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# include /alidata/server/nginx/conf/rewrite/phpmyadmin.conf;
#access_log /alidata/log/nginx/access/phpmyadmin.log;
}
這個(gè)啟動(dòng)完的顯示
----------------------- WORKERMAN -----------------------------
Workerman version:3.5.14 PHP version:7.0.0
------------------------ WORKERS -------------------------------
proto user worker listen processes status
tcp root none websocket://127.0.0.1:8686 4
----------------------------------------------------------------
Press Ctrl+C to stop. Start success.
網(wǎng)頁(yè)代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />
<title>撒的發(fā)撒的發(fā)</title>
</head>
<body class="gray-bg">
<script language="javascript">
ws = new WebSocket("wss://xxxxxxxxxx/wss");
ws.onopen = function() {
alert("連接成功");
ws.send('tom');
alert("給服務(wù)端發(fā)送一個(gè)字符串:tom");
};
ws.onmessage = function(e) {
alert("收到服務(wù)端的消息:" + e.data);
};
ws.onerror = function(e) {
console.log(e);
};
</script>
</body>
</html>
一直連接超時(shí)504
WebSocket connection to 'wss://xxxxxxx/wss' failed: Error during WebSocket handshake: Unexpected response code: 504
瀏覽器拋出的是這個(gè)錯(cuò)誤提示,我排查了一下有可能是通訊協(xié)議的問(wèn)題,但是我實(shí)在是不知道怎么改
按照手冊(cè)把nginx多余的配置去掉試下,沒(méi)問(wèn)題了再把nginx一點(diǎn)一點(diǎn)加回來(lái)看下是哪里問(wèn)題
那我滿足了 我的版本是1.7的 其次跨域的那個(gè)現(xiàn)在不用考慮 最后各種姿勢(shì)的配置文件我也嘗試過(guò)了但就是gg 各種504 是我臉黑嗎 還是我姿勢(shì)不對(duì)=.=
server {
listen 443 ssl;
server_name xxxxxx.com;
root /data/home/www/songshihuishou/public;
ssl on;
ssl_certificate /data/home/server/nginx-1.7/conf/ssl/1537639025967/1537639025967.pem;
ssl_certificate_key /data/home/server/nginx-1.7/conf/ssl/1537639025967/1537639025967.key;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
location /wss
{
proxy_pass http://127.0.0.1:8282;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
}
<?php
include __DIR__ . '/workerman/Autoloader.php';
use Workerman\Worker;
if(strpos(strtolower(PHP_OS), 'win') === 0)
{
exit("start.php not support windows, please use start_for_win.bat\n");
}
// 標(biāo)記是全局啟動(dòng)
define('GLOBAL_START', 1);
// 這里設(shè)置的是websocket協(xié)議(端口任意,但是需要保證沒(méi)被其它程序占用)
$ws_worker = new Worker('websocket://0.0.0.0:8282');
// 設(shè)置transport開啟ssl,websocket+ssl即wss
//$ws_worker->transport = 'ssl';
// 啟動(dòng)4個(gè)進(jìn)程對(duì)外提供服務(wù)
$ws_worker->count = 4;
// 當(dāng)收到客戶端發(fā)來(lái)的數(shù)據(jù)后返回hello $data給客戶端
$ws_worker->onMessage = function($connection, $data){
// 向客戶端發(fā)送hello $data
$connection->send('hello ');
};
Worker::runAll();
這個(gè)是后端的代碼
Workerman status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:3.5.14 PHP version:7.0.0
start time:2018-09-05 09:58:08 run 0 days 0 hours
load average: 0, 0, 0 event-loop:\Workerman\Events\Select
1 workers 4 processes
worker_name exit_status exit_count
none 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
19991 2M websocket://0.0.0.0:8282 none 0 0 0 0 0
19992 2M websocket://0.0.0.0:8282 none 0 0 0 0 0
19993 2M websocket://0.0.0.0:8282 none 0 0 0 0 0
19994 2M websocket://0.0.0.0:8282 none 0 0 0 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 8M - - 0 0 0 0 0
多謝多謝,我這邊也碰到類似的問(wèn)題,我的域名解析也用了cdn,請(qǐng)問(wèn)你是怎么解決的?