NativeScript Simple Networking
Basic UDP and TCP sockets for NativeScript.
Supported platforms
- Android (any device with Android 4.4 and higher)
There is no support for iOS (yet), as I am not an iOS developer. Contributions for adding iOS support are welcome!
Installing
tns plugin add nativescript-simple-networking
Usage
This plugin provides three classes: UdpServer
, TcpClient
and TcpServer
.
All of them provide similar, callback-based, interface. An example of usage is
worth a thousands words and therefore here is a TypeScript example:
;; var udpServer = ;udpServer { console;};udpServer { console;};udpServer { console;}; // Start listening on port 33333var udpConnectEvent: number = udpServerstart33333;console;// Broadcast a messagevar udpBroadcastEvent: number = udpServer;console; // Start a TCP server listening on port 44444 with maximum 2 clientsvar tcpServer = 2;tcpServer { console tcpServer;};tcpServer { console;};tcpServer { if client console; else console;};tcpServer { console;}; tcpServerstart44444; // Connect to the TCP servervar tcpClient = ;tcpClient { console;};tcpClient { console;};tcpClient { console;}; // Connect client, action IDs are ommited in this example - see UdpServertcpClientstart"localhost" 44444;tcpClient; // When we are finishedudpServer;TcpServer;TcpClient;
Contributing
Any contributions are welcome, feel free to submit a pull request on GitHub. I would appreciate a PR, which would add support for iOS.
Future Plans
- support iOS
- implement a wrapper for future-based interface