Welcome to the third part of our series introducing some of the new features coming up in Titanium Mobile 1.7.0. This second installment focuses on the new
Titanium.Network.Socket module and TCP socket implementation.
Currently, the Titanium Mobile API does not have a cross platform implementation for sockets, and the addition of the
Titanium.Network.Socket module and associated
Titanium.Network.Socket.TCP object will fill that void. Initially, only TCP sockets will be supported but UDP sockets will be added in the future. The addition of support for listening and connecting sockets via TCP will allow application developers to incorporate lower level network logic into their applications. Titanium.Network.Socket.TCP implements the Titanium
Stream interface and therefore relies on the previously mentioned
Socket operations are generally asynchronous and therefore socket callbacks are core to how one operation flows into the next. For example, when connecting to a remote host, you would wait until the
connected callback was invoked and fire off the read / write operations from inside that callback.
Once a socket is connected, IO operations leave the scope of Ti.Network.Socket and move into the realm of general Stream IO. Again, because sockets are naturally asynchronous, IO interactions with them should generally be conducted through the
Ti.Stream module. Lastly, while sockets can be an extremely powerful part of your developer tool box, network programming is non-trivial and investigation into the basics of how BSD sockets work is suggested.
Usage Examples: [gist id=948883] [gist id=948889] [gist id=948897]
Kitchen Sink examples: https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/socket_connect.js https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/socket_listener.js https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/socket_server_client.js
Socket Specification: http://wiki.appcelerator.org/display/guides/TCP+Socket+API+Spec Socket Unit Tests: https://github.com/appcelerator/titanium_mobile/blob/master/drillbit/tests/network.socket.tcp.js
Buffer Specification: http://wiki.appcelerator.org/display/guides/Buffer+Spec Buffer Unit Tests: https://github.com/appcelerator/titanium_mobile/blob/master/drillbit/tests/buffer.js
Stream Specification: http://wiki.appcelerator.org/display/guides/Stream+Spec Stream Unit Tests: https://github.com/appcelerator/titanium_mobile/blob/master/drillbit/tests/stream/stream.js