beego 框架搭建websocket聊天室
js websocket连接方法
function connect() {
ws = new WebSocket("wss://"+document.location.host+"/ws");
ws.onopen = onopen;
ws.onmessage = onmessage;
ws.onclose = function() {
console.log("连接关闭,定时重连");
connect();
};
ws.onerror = function() {
console.log("出现错误");
};
}
nginx配置websocket支持
location /ws {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_pass http://127.0.0.1:自己的端口/ws;
}
最终效果如图