https下的websocket链接问题
错误信息
An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
最近在做一个项目的时候遇到需要使用websocket进行异步消息通知,就是有消息了,后台立马弹个窗出来。
后台使用netty做的websocket,具体代码就省略了,使用的端口是8081,然后正常服务使用端口8080;
ScheduledUtils.threadPool.execute(() -> {
try {
new com.kuerle.api.netty.NettyServer(8081, sysUserMapper).start();
} catch ( Exception e ) {
e.printStackTrace();
}
});
前端的伪代码:
const hh = window.location.href
if (hh.indexOf(https) > -1) {
this.websock = new WebSocket(wss://域名/ws)
} else {
this.websock = new WebSocket(ws://ip地址:8081/ws)
}
我这里的入口是/ws,一般的是/websocket,不过没啥影响,按照自己的改
nginx配置
在配置文件加入:
location /ws {
proxy_pass http://127.0.0.1:8081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
ssl配置部分就省略了, chrome查看下请求 websocket成功链接.
以上。
