客户端websocket 连接:
$(function () {
var host = location.protocol === 'http:' ? "ws://xx.xx.xx.xx:80/websocket" :"wss://xx.xx.xx.xx:80/websocket";
try{
socket = new WebSocket(host);
socket.onopen = function (msg){
console.log('connected');
};
socket.onmessage = function (msg){
console.log(msg.data);
};
socket.onclose = function(msg){
console.log('socket closed');
};
}
catch (ex){
console.log(ex);
}
});
nginx.conf 增加配置:
location /websocket {
proxy_pass http://127.0.0.1:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}