knowledgecode / WebSocket-for-Android

Cordova/PhoneGap plugin that makes WebSocket available on Android
Apache License 2.0
143 stars 53 forks source link

[Question] Is it only for file:// or does it also work with sites in an iframe? #11

Closed Tarang closed 9 years ago

Tarang commented 10 years ago

Does the plugin still work if the index.html file has an iframe in it that connects to a website that uses websockets (via SockJS)?

I'm testing out Meteor apps with Cordova using an Android 4.2.1. I'm curious if this can work with it.

knowledgecode commented 10 years ago

Sorry, I have never tested Meteor and I haven't known very much about it. For now, I installed it, made a very simple app. It might be irrelevance, but it seems to work:

index.html(Cordova/PhoneGap):

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <title>Hello World</title>
    </head>
    <body>
        <script type="text/javascript" src="cordova.js"></script>
        <!-- This is my local server. -->
        <iframe src="http://192.168.1.7:3000/" style="width: 100%; height: 100%;"></iframe>
    </body>
</html>

app.html(Meteor):

<head>
  <title>app</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

app.js(Meteor):

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return "Welcome to app.";
  };

  Template.hello.events({
    'click input': function () {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

When I changed the greeting message("Welcome to app.") in app.js, I saw that it was reflected the change on my device soon:

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    //return "Welcome to app.";
    return "foo bar baz";
  };

About the following error message:

WebSocket is closed before the connection is established.

I guess this is a problem attributable to constitution of your app. So I will be never able to help you unless I will get you to show your app. Could you show me the snippet?

pawelfus commented 10 years ago

Well, I'm also trying to use this plugin to enable websockets in Android < 4.4 and to use them inside iframe. Some code snippets:

Create websocket:

var host = location.origin.replace(/^http/, 'ws');
var socket = scope.websocket = new ReconnectingWebSocket(host);

Where ReconnectingWebSocket is simply creating websocket (and taking care to reconnect in some cases):

var ws = new WebSocket(url, []);
ws.onmessage = function () { ... }  //etc.

I guess I've made some mistake while installing plugin. How to make sure it's properly installed? In some config files should exist some entry? Or there indeed we have some issue with iframes?

Note: I'm using native WebSockets, no extra plugin like socketIO or sockJS.

knowledgecode commented 10 years ago

To make sure the plugin is properly installed, type as below on your project root directory:

$ cordova plugin

If you get a message as below, it means it has been succeeded in installing:

com.knowledgecode.cordova.websocket 0.x.x "WebSocket for Android"

Why don't you test it without iframe to isolate the problem?

knowledgecode commented 9 years ago

I heard that Meteor had supported Cordova/PhoneGap: https://github.com/meteor/meteor/blob/devel/History.md

So I think that this issue is not need to remain open any more. Best regards,

pawelfus commented 9 years ago

Sorry, couldn't find time to investigate issue further. Note: I'm not using Meteor ;) Probably next month I will have time to check this, sorry.

knowledgecode commented 9 years ago

Oh, I judged prematurely. I will await your reply.

SuryaAdabala commented 9 years ago

[Question] I am new to github and I do not know where to ask questions, So I am asking here, help me out on this issue,

I added WebSocket plugin to my android project and made changes on js file on cordova 3.6.3 and android 4.3, but I am not able to create WebSocket connection.Is there anything I need to add on native side to initiate WebSocket connection.

Thanks in Advance.

pawelfus commented 9 years ago

I totally forget about this issue. We resigned from Cordova after all - as for me, ticket can be closed.

knowledgecode commented 9 years ago

I'm finally able to close this.

pawelfus commented 9 years ago

Yeah, I know that feeling.. Sorry for a delay! :)