使用PhoneGap的在Android应用程序的WebSocket不工作 [英] Websocket not working in android app using phonegap

查看:218
本文介绍了使用PhoneGap的在Android应用程序的WebSocket不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立我的signalR服务器和Android应用程序,使用PhoneGap的CLI建之间的WebSocket连接。

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

JavaScript的code在我的电脑上的浏览器运行,但是当我打包为Android无法连接,并提供了以下错误:协商请求时出错

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

下面是JavaScript code -

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>

在asp.net code托管在Azure上。
我也试过不生成的代理连接(评论code)其中Chrome,但不是在Android模拟器(4.4)。

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?

感谢

推荐答案

我有同样的问题,因为你。
的WebSocket是HTML5中的一个特点,不是所有的浏览器都支持
Chrome浏览器支持的WebSockets但是Android 4.4(果冻豆)的浏览器不支持。
这就是为什么Android的开发奇巧(支持WebSocket的)。
如果你想使用Android 4.2,4.4(糖豆)内的WebSocket,你必须使用的WebSocket科尔多瓦插件像一个的这里
还有很多其他的,我就跟你试图寻找一个答案

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

这篇关于使用PhoneGap的在Android应用程序的WebSocket不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆