赞
踩
Websocket使用 ws 或 wss 的统一资源标志符,类似于 HTTP 或 HTTPS,其中 wss 表示在 TLS 之上的 Websocket ,相当于 HTTPS 了。
默认情况下,Websocket 的 ws 协议使用 80 端口;运行在TLS之上时,wss 协议默认使用 443 端口。其实说白了,wss 就是 ws 基于 SSL 的安全传输,与 HTTPS 一样样的道理
配置Nginx支持wss
# 我的配置在https的nginx下面
location /usign {
proxy_pass http://127.0.0.1:9990/;
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 REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 5000M;
}
代码中的使用
vue中配置
我这里用在了vue里面,工程使用的脚手架。websocket用在了app.vue里面,因为我的是全局的监听。从服务器上面回来的数据,可以通过vue的store来改变其他页面的数据
import { mapState, mapMutations } from 'vuex'
export default {
name: 'App',
data () {
return {
websock: null,
reconnectData: null,
lockReconnect: false,
timeout: 10000,
timeoutObj: null,
serverTimeoutObj: null
}
},
computed: {
...mapState([
'currentUser',
'identificationDevice',
'logshow'
])
},
created () {
document.title = ''
this.initWebSocket()
},
methods: {
...mapMutations([
'updatePromptBtn',
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。