Websocket not working in android app using phonegap

柔情痞子 提交于 2019-12-08 09:58:41

问题


I am trying to establish a websocket connection between my signalR server and an android app, built using the phonegap CLI.

The javascript code runs on browsers on my PC but when I package it for android it fails to connect and gives the following error: Error during negotiation request

Here is the javascript code -

<!DOCTYPE html>
<html>
<head>
    <title>My New Application</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no;" />

    <script type="text/javascript" src="js/jquery-1.7.1.js" ></script>
    <script type="text/javascript" src="js/jquery.mobile-1.0.1.js"></script>
    <script type="text/javascript" src="js/jquery.signalR-2.0.3.js"></script>  
    <script type="text/javascript" src="phonegap.js"></script>

    <script type="text/javascript" src=http://WEB_ADDRESS.net/signalrPush/signalr/hubs"></script>

    <script type="text/javascript"> 
        $(function() {

            alert('Phonegap device ready event...');

            /*
            var connection = $.hubConnection("http://WEB_ADDRESS.net/signalrPush/signalr", { useDefaultPath: false });
            connection.error(function (error){
                alert("SignalR error: " + error);
                });
            var pushhubProxy = connection.createHubProxy('pushhub');
            pushhubProxy.on('sendmsg',function(message){ $('#ulServerMessages').append('<li>' + message + '</li>'); alert(message);});
            connection.start({ transport: ['webSockets', 'longPolling'] }).done(function(){ alert('Now connected, connection ID=' + connection.id);})
                            .fail(function(){ alert('Could not connect'); });

            */

            $.connection.hub.url = "http://WEB_ADDRESS.net/signalrPush/signalr";
            var mypushHub = $.connection.pushhub;
            if(typeof(mypushHub)=="object")
            {
                alert(typeof(mypushHub));   
                mypushHub.client.sendmsg = function (message) {
                    $('#ulServerMessages').append('<li>' + message + '</li>');
                    alert(message); 
                }
                $.connection.hub.start({jsonp: true}).done(function () {
                    mypushHub.server.broadcastmsg();
                }).fail(function (error) { alert(error); });
            }
            else
            {
                alert(typeof(mypushHub));
                alert("Connection Prob");
            }
        });
    </script>
</head>
<body>
    <div data-role="header">
        <h1>Get Server Data</h1>
    </div>
    <div id="pusheddata" style="width:300px; height:400px; overflow: auto;">
       <ul id="ulServerMessages"></ul>
    </div>
</body>
</html>

The asp.net code is hosted on azure. I also tried to connect without the generated proxy(commented code) which again worked on chrome but not on the android emulator(4.4).

Could someone tell me what I am doing wrong?

Thanks


回答1:


I am having the same problem as you. Websocket is a feature within html5 and is not supported by all browsers Chrome browser supports websockets but android 4.4 (jelly beans) In-browser doesn't. This is why Android developed kitkat (which supports the websocket). If you want to use websocket within android 4.2,4.4 (jelly beans), you have to use websocket cordova plugin like the one here there are alot of others and I am just like you trying to find an answer



来源:https://stackoverflow.com/questions/24485530/websocket-not-working-in-android-app-using-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!