TCPSocket and node.js: socket write() does not work
i'm trying to develop a library which would allow functionality similar to Titanium.API.log, but would be usable from an actual device, which is not connected to the development environment, but only connected to a network (btw, i'm planning to publish the source). node.js is used as a "listening" server component.
problem:when i open tcpsocket on the device with the WRITE_MODE, or READ_WRITE_MODE, and trying to write to the ip/port - nothing happens on the server side, i.e. the write is not being detected on the server at all. everything else works: connection happens and stays open until closed, "write" from the server to the device's socket also works and correctly triggers "read" event on the device.
question:what needs to be done to make tcpsocket "write" actually write to the socket?
2 Answers
-
I got the following echo server in node.js and simple app in titanium working just now.
node.js echo server
var net = require('net'); net.createServer(function(socket) { socket.addListener('data', function(data) { socket.write(data); }) }).listen(8001);
Titanium App
<html> <head> <script> var tcpsock = null; window.onload = function () { var myWindow = Titanium.UI.getCurrentWindow(); myWindow.addEventListener(Titanium.CLOSE, function(event) { Titanium.App.exit(); }); }; var connect = function () { tcpsock = Titanium.Network.createTCPSocket(document.getElementById('hostname').value, parseInt(document.getElementById('port').value)); tcpsock.onRead(function(data) { document.getElementById('received').value += data + "\n"; }); tcpsock.connect(); }; var disconnect = function () { tcpsock.close(); }; var sendit = function () { tcpsock.write(document.getElementById('to_send').value); document.getElementById('to_send').value = ''; }; </script> </head> <body style="background-color: #1c1c1c; margin: 0;"> <div style="border-top: 1px solid #404040"> <div style="color: #fff; padding: 10px;"> Host: <input type="text" size="25" id="hostname"></input>Port: <input type="text" size="5" id="port"></input><button type="button" onclick="connect();">Connect</button><button type="button" onclick="disconnect();">Disconnect</button><br /> <textarea rows="03" cols="40" id="to_send"></textarea><button type="button" onclick="sendit();">Send</button><br /> <textarea rows="10" cols="40" id="received"></textarea><br /> </div> </div> </body> </html>
I hope that helps you!
-
it is fully working now.
i've designed remote debugging system based on tcpsockets available at http://www.cloudebug.com.
as a part of the code provided with this system you will find a working tcpClient class which you can use in your projects freely.
here is the link to the post:
http://developer.appcelerator.com/question/101611/new-debugging-system-for-titanium—cloudebug
or, you can go to http://www.cloudebug.com, register and get the source code for the tcpClient class.
good luck!
yury