1

What should I do in order to use websocket in my ionic app? I couldn't find any info in docs about this.

I'm just writing in my code:

.controller('Controller', function($scope) {
  $scope.planets = [{'name': 'Меркурий', 'order': '1'},
                    {'name': 'Венера', 'order': '2'},
                    {'name': 'Земля', 'order': '3'},
                    {'name': 'Марс', 'order': '4'},
                    {'name': 'Юпитер', 'order': '5'},
                    {'name': 'Сатурн', 'order': '6'},
                    {'name': 'Уран', 'order': '7'},
                    {'name': 'Нептун', 'order': '8'},
                    {'name': 'Плутон', 'order': '9'},
                   ];

  $scope.sendData = function(selected) {
      var planets = $scope.planets;
      var num;
      for (var i=0; i < planets.length; i++) {
          if (planets[i].name === selected) {
              num = planets[i].order;
          } 
      }

      var socket = new WebSocket("ws://somesource.com");
      socket.send(num);
      socket.onmessage = function(data) {
          alert(data);
      };

  };

})

And nothing...

By design somesource.com must return me some data responding on my "num".

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Dennis
  • 487
  • 5
  • 15

1 Answers1

2

It's likely you would have found something if you had searched for WebSockets on Cordova instead of Ionic (Cordova is an underlying component of Ionic).

WebSockets are not officially supported because Android has no native WebSocket support and only the newest iOS devices have native WebSocket support:

   WebSockets or an alternative with phonegap?

   https://www.quora.com/Does-PhoneGap-support-WebSocket

As an alternative, I recommend following this tutorial to learn how to use Socket.IO with Ionic:

   https://www.sitepoint.com/using-socket-io-and-cordova-to-create-a-real-time-chat-app/

Community
  • 1
  • 1
Lightbeard
  • 4,011
  • 10
  • 49
  • 59