JS
// 创建 WebSocket 对象
var socket = new WebSocket('wss://网站域名/ws');
// 连接建立后的回调
socket.addEventListener('open', function (event) {
console.log('WebSocket connection established.');
});
// 当从服务器接收到消息时触发
socket.addEventListener('message', function (event) {
if(event.data=="sms"){
window.location.href = "/online.php?page=1";
}
});
// 连接关闭时的回调
socket.addEventListener('close', function (event) {
console.log('WebSocket connection closed.');
});
// 发送数据到服务器
socket.send('Hello, Server!');
nginx 配置
location /ws {
proxy_pass http://127.0.0.1:8800; # 后端 WebSocket 服务的地址
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}