话不多说 上代码

 var httpswebsocket='';
 var httpwebsocket='';
 if(document.location.protocol=='https:'){ws = new WebSocket("wss://"+httpswebsocket);}else{ ws = new WebSocket("ws://"+httpwebsocket);}