LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Returning value of extensive javascript function with WebApp NXG

I have a Javascript websocket script that connects to websocket and then downloads data until the target closes the socket.  I'm trying to return the data as a string after this function has run, however the JSLI node on the block diagram gets passed through instantly instead of waiting for the data to be downloaded. Perhaps there is something wrong with the javascript coding.  Note: I know the javascript works in terms of downloading the data as I see it being posted to the chatLog, I just am having trouble figuring out how to return all of that data to LabVIEW environment.  I am not sure if the return data is done correctly in the javascript.

 

Expected: call the connect function through jsli on the block diagram, wait for javascript to run, return data variable as string

Actual: jsli node instantly returns empty string, no errors present

 

Javascript

// This is the JavaScript file for the basic LabVIEW WebSockets demo.
// Copyright © 2016 by MediaMongrels Ltd. 

	function connect(){  
		var socket;
		const data = "";
			
		
		// The LabVIEW demo uses port 6123, but this can be changed to any port. Change 'localhost' if connecting to a server running on another IP address.
		var host = "ws://10.0.4.108:6124";  

		try{  
			var socket = new WebSocket(host);  
  
			message('<p class="event">Connecting...');  
			
			// Tell the user the connection has been established
			socket.onopen = function(){  
				message('<p class="event">Connected');
				message('<p class="event">Downloading Data...');				
			}  
			
			// Display the received message
			socket.onmessage = function(msg){  
				message('<p class="message">'+msg.data);
				data.concat(msg.data);
			}  
			
			// Tell the user the connection has been closed
			socket.onclose = function(){			
				message('<p class="event">Socket Status: '+socket.readyState+' (Closed)'); 
				message('<p class="event">' +typeof data); 
						
			
			}           
  
		} catch(exception){  
			message('<p>Error'+exception);  
		}    
		
		// Add the message to the log (and close the paragraph)
		function message(msg){  
			$('#chatLog').append(msg+'</p>');  
		}  
   	return data;
	} //End connect

 

 

 

JSLI global:image.png

 

Picture of JSLI node on block diagram:

image.png

 

 

 

----------------------------------------------------
Studying for CLA.
LabVIEW, inherit from social media habits!
0 Kudos
Message 1 of 2
(1,447 Views)

I found a much easier solution, I forgot that the reason I used JavaScript for websocket was to give everyone easy access to the data.  Well duh, NXG WebApp covers that now. Here is what I replaced the JavaScript Node with (problem solved, I don't have to learn so much javascript):

CDuck_0-1598384236320.png

 

----------------------------------------------------
Studying for CLA.
LabVIEW, inherit from social media habits!
Message 2 of 2
(1,413 Views)