Why would you use the TCPClient? For single remote operation you could leave the socket open. I do something like this: var self = this; this.ip = a; this.Command = function(a){ self.socket.write(a + "\r"); }; this.Command.Power = {}; this.Command.Power.On = function(){ self.Command("PWON"); }; this.Command.Power.Off = function(){ self.Command("PWSTANDBY"); }; this.Command.Power.GetStatus = function(){ self.Command("PW?"); }; this.Command.Power.Status = ""; this.socket = new TCPSocket(false); this.socket.connect(self.ip, 23, 3000); this.socket.Reconnect = function(){ self.socket = new TCPSocket(false); self.socket.connect(self.ip, 23, 3000); }; this.socket.onIOError = function(){ Activity.scheduleAfter(5000, self.socket.Reconnect); }; this.socket.onClose = function(){ Activity.scheduleAfter(5000, self.socket.Reconnect); }; this.Poll = function(){ if (self.socket.connected === true) { self.Command.Power.GetStatus(); System.delay(10); self.Command.ZoneMain.Source.GetStatus(); System.delay(10); self.Command.ZoneMain.Volume.GetStatus(); System.delay(10); self.Command.ZoneMain.SurroundMode.GetStatus(); System.delay(10); self.Command.ZoneMain.Mute.GetStatus(); System.delay(10); self.Command.RoomEQ.GetStatus(); System.delay(10); } else { Activity.scheduleAfter(1000, self.Poll); } }; this.socket.onData = function(){ a = self.socket.read(); var b = a.split("\r"); for (var i = 0; i < b.length; i++) { switch (b[i].substr(0, 2)) { case "MV": if (b[i].substr(0, 3) !== "MVM") { self.Command.ZoneMain.Volume.Status = self.parseVolume(b[i]); } break; case "MS": self.Command.ZoneMain.SurroundMode.Status = self.parseSurround(b[i]); break; case "MU": self.Command.ZoneMain.Mute.Status = self.parseMute(b[i]); break; case "PW": self.Command.Power.Status = self.parsePower(b[i]); break; case "SI": self.Command.ZoneMain.Source.Status = self.parseInput(b[i]); break; case "PS": self.Command.RoomEQ.Status = self.parseRoomEQ(b[i]); break; } } self.UpdateGUI(); };
This code needed some rewrite. Socket re-use is not recommended like this and the code is faster in object style (like: this.Action={act1:function(){self.Command("Action1");},act2:function(){self.Command("Action2");}};) .
Last edited by sWORDs on September 12, 2010 17:12.