<script type="text/javascript">
var ws = new WebSocket("ws://38.55.97.143:6162/"); # var ws = new Websocket(wss://pp111.yoongle.com/wss);
ws.onopen = function(event) {
console.log("WebSocket connection opened");
// 可以在这里发送数据给服务器
ws.send("Hello, server!");
};
ws.onmessage = function(event) {
console.log("Received message from server: " + event.data);
};
ws.onclose = function(event) {
console.log("WebSocket connection closed");
};
ws.onerror = function(error) {
console.error("WebSocket error observed:", error);
};
</script>
socket后台nginx配置
location /wss { # WebSocket请求的路径
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:6162; # WebSocket服务的地址和端口
}
发表评论 取消回复