Search code examples
androidsocketstitaniumsockjs

Titanium Appcelerator has no method createTCp


I'm trying to create a socket connection to SockJS server and following the example given here but Titanium keeps on giving me the error "has no method createTCP"

This is the code I'm using

var connectingSocket = Ti.Network.createTCP({
host: host,
port: port,
connected:function(e) {
    e.socket.write(Ti.createBuffer({data: "Well, hello there!"}));
},
error:function(e) {
    Ti.UI.createAlertDialog({
        title:"Socket error: "+e.errorCode,
        message:e.error
    }).show();
    Ti.API.info("CONNECTION has been closed: "+e.socket.host+":"+e.socket.port);
}
});
connectingSocket.connect();

Solution

  • Use Ti.Network.Socket.createTCP instead of Ti.Network.createTCP.

    Here is the structure of code, as example.

    var clientSocket = Ti.Network.Socket.createTCP({
    
        host : hostname,
        port : port,
        connected : function(e) {
    
            e.socket.write(Ti.createBuffer({
                value : 'A message from a connecting socket.'
            }));
        },
        error : function(e) {
            Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
        }
    });
     clientSocket.connect();