Tcp socket on ionic

Hi,

I want to make tcp socket client on ionic. So I found https://github.com/nutella/ionic-cordova-chrome-tcp-example.
My problem is " TypeError: Cannot read property ‘tcp’ of undefined"

It seems not to create “chrome.sockets”. and in my html, I added below script as sample but not thing logged.

<script type="text/javascript">
	document.addEventListener('deviceready', function onDeviceReady() {
		console.log(chrome);
    	angular.bootstrap(document, ['starter']);
	}, false);
</script>

In my test, I added just the ng-cordova.min.js and cordova plugin add cordova-plugin-chrome-apps-sockets-tcp …

I’m testing this with browser by issuing ionic serve.

I don’t have any clue to fix this. Below code almost similar. But If I wrong, Please let me know.

App.js is

angular.module('starter', ['ionic', 'starter.controllers','ngCordova'])
.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      StatusBar.styleDefault();
    }
  });

})

My controller is

angular.module('starter.controllers', [])
.controller('TcpCtrl', function($scope,tcpServices) {

    var vm= this;
    vm.send= send;

  init();

  function init() {
 
    vm.result= '';
  }

  $scope.test=function(){
          tcpServices.sendPacket("127.0.0.1",1234,"abcd").then(function(data) {
      vm.result= data;
    });
  }
})

My service.js

angular.module('starter').factory('tcpServices', tcpServices);

tcpServices.$inject= ['$q'];

function tcpServices($q) {

console.log("here tcpServices");

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

	function str2arrayBuffer(str) {
		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;
	}

	function sendPacket(ipAddr,ipPort,data) {

		console.log("here tcpServices' sendPacket",ipAddr,ipPort);

	  var delay= 5000;	/// 5 seconds timeout
        var deferred= $q.defer();
		chrome.sockets.tcp.create({}, function(createInfo) {
			var _socketTcpId= createInfo.socketId;
			chrome.sockets.tcp.connect(_socketTcpId, ipAddr, ipPort, function(result) {  // <-- Error
				if (result === 0) {
					var data2send= str2arrayBuffer(data);
					/// connection ok, send the packet
					chrome.sockets.tcp.send(_socketTcpId, data2send);
				}
			});
			chrome.sockets.tcp.onReceive.addListener(function(info) {
				/// recived, then close connection
				chrome.sockets.tcp.close(_socketTcpId);
				var data= arrayBuffer2str(info.data);
				deferred.resolve(data);
			});
			/// set the timeout
			setTimeout(function() {
				chrome.sockets.tcp.close(_socketTcpId);
                deferred.reject();
			}, delay);
		});
        return deferred.promise;
	}

	return {
		sendPacket: sendPacket
    };
};

You can’t test this in browser, only on real device.

1 Like
  • ionic.bundle.js:26794 TypeError: Cannot read property ‘tcp’ of undefined
  • at Object.sendPacket (tcp-services.js:32)
    
  • at Scope.$scope.send (controllers.js:21)
    
  • at fn (eval at compile (ionic.bundle.js:27638), <anonymous>:4:203)
    
  • at ionic.bundle.js:65427
    
  • at Scope.$eval (ionic.bundle.js:30395)
    
  • at Scope.$apply (ionic.bundle.js:30495)
    
  • at HTMLButtonElement.<anonymous> (ionic.bundle.js:65426)
    
  • at defaultHandlerWrapper (ionic.bundle.js:16787)
    
  • at HTMLButtonElement.eventHandler (ionic.bundle.js:16775)
    
  • at triggerMouseEvent (ionic.bundle.js:2953)(anonymous function) @ ionic.bundle.js:26794(anonymous function) @ ionic.bundle.js:23507$apply @ ionic.bundle.js:30500(anonymous function) @ ionic.bundle.js:65426defaultHandlerWrapper @ ionic.bundle.js:16787eventHandler @ ionic.bundle.js:16775triggerMouseEvent @ ionic.bundle.js:2953tapClick @ ionic.bundle.js:2942tapTouchEnd @ ionic.bundle.js:3069

…then? Give us some info about your error… What code do you use? What version?