Which should look like this instead - Processing code moved inside the OnData and obvious bugfix in connect - I have also taken liberties of pointing out what I believe may be potential problems you will encounter:
var result=""; // note change to empty string. var socket= new TCPSocket(false); socket.connect("192.168.10.35",8005,3000); socket.onConnect = function() { result = ""; // reset buffer. socket.write("stringtosend"); }; socket.onData = function() { result+=socket. read(); // Note that this if clause moved inside of the function. // Note that result may have trailing Carriage Return and if so, this clause will never report true. if (result== "05380001013F") { socket.write("\\053801007901\r"); } else { // you may have to account for incomplete data here and not do this write. socket.write("\\053801000101\r"); } socket.close();
};
socket.onIOError = function(e) { // Is this a bug? Presume label1 is a widget so may need to be label1.label label1 = "socket error:"+e; };