How to create a websocket connection in my custom JS file and multiple components can use socket.send function
[issue link]case 1:
custom JS file
var wsUrl = 'ws://localhost:8080';
var socket = new WebSocket(wsUrl);
Nuxt.js Error:
WebSocket is not defined
case2
custom JS file
if(typeof(window) !== 'undefined'){
var wsUrl = 'ws://localhost:8080';
var socket = new WebSocket(wsUrl);
}
export {socket};
.vue file
import { socket } from '~assets/js/socket.js';
...
mounted(){
console.log(socket); //undefined
}
...
chrome console:
undefined