0

I have a meteor application running on port 3000.
Then I have a ionic application serving a simple client.
I want to see the data adeed in the meteor application in in the ionic application.
I created a service:

(function () {
angular.module('myApp').factory('myApi', ['$q', '$rootScope', myApi]);
function myApi($q, $rootScope) {
    var options = {
        endpoint: "ws://localhost:3000/websocket",
        SocketConstructor: WebSocket
    };
    var ddp = new DDP(options);
    ddp.on("connected", function () {
        console.log("Connected");
        ddp.sub("allHistory");
    });

    function on(callback) {
        ddp.on("added", function (data) {
            console.log("on added", data.fields.value);
            var item = { value: data.fields.value, date: data.fields.date };
            callback(item);
        });
    };
    return {
        on: on,
    };
}
})();

A controller:

angular.module('myApp').controller('MyController',
function EventController($scope, myApi) {
   myApi.on(function (data) {
        $scope.history.push(data);
    });
    $scope.history = [];
}
);

A view:

 <div ng-controller="MyController" style="padding-left:20px; padding-right:20px">
    <ul class="thumbnails">
        <li ng-repeat="item in history">
            <div class="row span9">
               <span>Date: {{item.date}}</span>
               <span>Value: {{item.value}}</span>
            </div>
        </li>
    </ul>
</div>

Everything works using a web client: I can to get data from the meteor application (using the DDP protocol with the ddp.js implementation) on a web client running the command ionic serve.
When I try a ionic emulate android I cannot see the data in the AVD and I have a 'ReferenceError: WebSocket is not defined' in the LogCat panel.
I tried to install the 'cordova plugin add https://github.com/knowledgecode/WebSocket-for-Android.git' but without success. How I can use websockets within a ionic application with the andorid target?

4

3 回答 3

0

好的,所以,我终于让它为我工作了,使用SockJS,它需要安装在服务器和客户端上,而不是使用这个代码:

var options = {
    endpoint: "ws://localhost:3000/websocket",
    SocketConstructor: WebSocket
};
var ddp = new DDP(options);
ddp.on("connected", function () {
    console.log("Connected");
});

我使用了这段代码:

  var options = {
      endpoint: "https://localhost:3000/echo", // echo is the prefix i set on server-side
      SocketConstructor: SockJS
  };
  var ddp = new DDP(options);
  ddp.on("connected", function () {
      console.log("Connected");
  });

所以,现在它连接了。但是当我将它部署到移动设备时,我现在得到一个“Access-Control-Allow-Origin”未设置的东西,在浏览器上它工作得很好,但在 Android 设备上它给了我这个错误......我无法评论关于问题,还没有足够的声誉,所以我很抱歉在回答中问另一个问题......但是有什么办法可以解决这个“访问控制......”的事情吗?

谢谢你,我希望我能以某种方式帮助。

于 2015-02-10T18:36:17.337 回答
0

在浏览器中工作的最可能原因是您的服务器和浏览器位于同一主机上。尝试通过浏览器使用来自不同主机的服务。你会得到同样的错误。您需要允许服务器端的标头修复“访问控制...”的东西

于 2015-02-27T08:09:28.917 回答
0

出于安全原因,Android 禁止通过套接字发送纯文本。如果您使用 chrome://inspect 调试代码,您会看到它会引发 ERR_CLEARTEXT_NOT_PERMITTED 错误。您可以建立与 SSL 的连接或告诉 android 允许明文流量。为此,请在声明下方写入 ...resources\android\xml\network_security_config.xml 文件。(192.168.1.3 和 192.168.2.3 是套接字服务器 ip)

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">localhost</domain>
            <domain includeSubdomains="true">192.168.1.3</domain>
            <domain includeSubdomains="true">192.168.2.3</domain>
        </domain-config>
    </network-security-config>
于 2020-05-08T20:01:13.877 回答