Quantcast
Channel: Ionic Forum - Latest posts
Viewing all articles
Browse latest Browse all 229340

TCP sockets in ionic

$
0
0

Hi guy,
I’m a new development guy, and I’ve found a solution and I give it.

export class HomePage  {

private socketTcpId: number;

constructor(platform:Platform , public navCtrl: NavController, private vibration: Vibration,   public alertCtrl: AlertController) {
        
        platform.ready().then((readySource) => {
	     if(readySource=='cordova'){
             
	     	 // start handling network stuff now
             //this.AlertAction();
             (<any>window).chrome.sockets.tcp.create({}, createInfo => {
             this.socketTcpId = createInfo.socketId;   
                // this.AlertAction();;
                (<any>window).chrome.sockets.tcp.connect(this.socketTcpId, "192.168.1.42", 12345, result => {
                console.log("Connected to server");
            });
            });
	     }
      }); 	
  }

And Send Method

arrayBuffer2str(buf): string {
		var str= '';
		var ui8= new Uint8Array(buf);
		for (var i= 0 ; i < ui8.length ; i++) {
			str= str+String.fromCharCode(ui8[i]);
		}
		return str;
   }

   str2arrayBuffer(str) : ArrayBuffer {
		var buf= new ArrayBuffer(str.length);
		var bufView= new Uint8Array(buf);
		for (var i= 0 ; i < str.length ; i++) {
			bufView[i]= str.charCodeAt(i);
  		}
		return buf;
	}

	sendPacket( ) : void {
		      
				var data2send= this.str2arrayBuffer( "Philippe Letenneur is the best\n\r");
				// connection ok, send the packet
				(<any>window).chrome.sockets.tcp.send(this.socketTcpId, data2send);
		
	}

Enjoy it :slight_smile:


Viewing all articles
Browse latest Browse all 229340

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>