Every line of 'obs-websocket-js' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.
114 function setWebsocket(ws) { 115 console.warn("setting new websocket"); 116 if (websocket !== null) { 117 websocket.close(); 118 } 119 websocket = ws; 120 websocket.on('message', function (message) { 121 var m; 122 //console.log('received: %s', message); 123 try { 124 m = JSON.parse(message); 125 //console.log('parsed version:'); 126 //console.log(m); 127 doCommand(m.id, m.namespace, m.command, m.args, m.isAsync, ws); 128 } catch (e) { 129 console.log("Error: could't parse the message or something"); 130 } 131 }); 132 }
28 private createWebSocket() { 29 this.ws = new WebSocket(this.options.url); 30 this.ws.onmessage = this.onMessage.bind(this); 31 32 if (this.options.onclose) { 33 this.ws.onclose = this.onClose.bind(this); 34 } 35 36 if (this.options.onerror) { 37 this.ws.onerror = this.onError.bind(this); 38 } 39 40 if (this.options.onopen) { 41 this.ws.onopen = this.onOpen.bind(this); 42 } 43 }
68 public connect() { 69 console.log('connect'); 70 let addr = this.addr; 71 try { 72 this.socket = new WebSocket(addr); 73 } catch (error) { 74 console.warn("connect error", error); 75 //KBEngine.Event.fire('onConnectionState', false); 76 return; 77 } 78 79 this.socket.onopen = this.onopen.bind(this); 80 this.socket.onerror = this.onerror_before_onopen.bind(this); 81 this.socket.onmessage = this.onmessage.bind(this); 82 this.socket.onclose = this.onclose.bind(this); 83 84 }
16 function createSocket(host){ 17 if(window.WebSocket) return new WebSocket(host); 18 else if(window.MozWebSocket) return new MozWebSocket(host); 19 }
45 function send(object) { 46 if (socket.readyState === 1) { // OPEN 47 socket.send(JSON.stringify(object)); 48 } else { 49 log(`Tried to send to WebSocket in readyState: ${socket.readyState}`) 50 } 51 }
16 function _connectWebSocket(uri) { 17 console.log("connect " + Pointer_stringify(uri)); 18 var handle = Module.webSocketsList.length; 19 Module.webSocketsList.push(new WebSocket(Pointer_stringify(uri))); 20 Module.webSocketsList[handle].onopen = function (event) { 21 console.log("onopen"); 22 }; 23 Module.webSocketsList[handle].onmessage = function (event) { 24 _passMessageToClient(handle, Module.toCharStar(event.data)); 25 }; 26 Module.webSocketsList[handle].onerror = function (event) { 27 console.log("onerror"); 28 }; 29 Module.webSocketsList[handle].onclose = function (event) { 30 console.log("onclose"); 31 }; 32 33 //this will become interval object later, see _heartbeat 34 Module.heartbeatIntervals.push("for later"); 35 36 return handle; 37 }
17 initializeWebSocket(url) { 18 console.log('OSC Controller connecting to: ', url); 19 20 this.websocket = new WebSocket(url); 21 this.websocket.onopen = this.onOpen.bind(this); 22 this.websocket.onclose = this.onClose.bind(this); 23 this.websocket.onmessage = this.onMessage.bind(this); 24 this.websocket.onerror = this.onError.bind(this); 25 26 // stop Chrome from ruining things and crashing the socket server 27 window.addEventListener('beforeunload', () => { 28 this.websocket.close(); 29 }); 30 }
22 private onConnection(ws: ws): void { 23 // Subscribe to WebSocket events. 24 ws.on('message', (message: string) => this.onMessage(message)); 25 ws.on('error', (error: Error) => this.onError(error)); 26 ws.on('close', () => this.onClose()); 27 28 console.log(`Connections count: ${this.wsServer.clients.size}`); 29 }
132 _onConnection(ws, req) { 133 let remoteAddress = req.connection.remoteAddress; 134 if (!remoteAddress) { 135 Log.e(WebSocketConnector, 'Expected req.connection.remoteAddress to be set and it is not: closing the connection'); 136 ws.close(); 137 return; 138 } 139 140 // If we're behind a reverse proxy, the peer's IP will be in the header set by the reverse proxy, not in the req.connection object 141 if (this._networkConfig.reverseProxy.enabled) { 142 const reverseProxyAddresses = this._networkConfig.reverseProxy.addresses; 143 if (reverseProxyAddresses.includes(remoteAddress)) { 144 const reverseProxyHeader = this._networkConfig.reverseProxy.header; 145 if (req.headers[reverseProxyHeader]) { 146 remoteAddress = req.headers[reverseProxyHeader].split(/\s*,\s*/)[0]; 147 } else { 148 Log.e(WebSocketConnector, `Expected header '${reverseProxyHeader}' to contain the real IP from the connecting client: closing the connection`); 149 ws.close(); 150 return; 151 } 152 } else { 153 Log.e(WebSocketConnector, `Received connection from ${remoteAddress} when all connections were expected from the reverse proxy at ${reverseProxyAddresses}: closing the connection`); 154 ws.close(); 155 return; 156 } 157 } 158 159 try { 160 const netAddress = NetAddress.fromIP(remoteAddress, true); 161 const conn = new NetworkConnection(new WebSocketDataChannel(ws), this._protocol, netAddress, /*peerAddress*/ null); 162 163 /** 164 * Tell listeners that an initial connection to a peer has been established. 165 * @event WebSocketConnector#connection 166 */ 167 this.fire('connection', conn); 168 } catch (e) { 169 Log.e(WebSocketConnector, `Error on connection from ${remoteAddress}: ${e.message || e}`); 170 ws.close(); 171 } 172 }
29 function WebSocket (mng, data, req) { 30 Transport.call(this, mng, data, req); 31 var self = this; 32 this.wsclient = req.wsclient; 33 req.wsclient.onerror = function(){ 34 self.end('socket error'); 35 }; 36 req.wsclient.onclose = function(){ 37 self.end('socket end'); 38 }; 39 req.wsclient.onmessage = function(ev){ 40 self.onMessage(parser.decodePacket(ev.data)); 41 }; 42 };