简体   繁体   中英

Websocket not working in android app using phonegap

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM