0

我正在使用phonegap在android中实现套接字编程。但我没有从服务器获取任何数据。实际上,我的 PC 是作为生产者的消费者。所以我只访问我的 IP 地址,但没有收到任何错误。但没有得到任何响应。请帮我 。...

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>websocket-android-phonegap</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
    <script type="text/javascript" charset="utf-8" src="js/websocket.js"></script>
  </head>
  <body>


  </body>

<script type="text/javascript" charset="utf-8">
    // new socket
    alert("Start");
    //var socket = new WebSocket('ws://192.168.12.171:8101/');
    var socket = new WebSocket('http://192.168.12.171:8001');

    // push a message after the connection is established.
    socket.onopen = function() {
    alert("Open")
        //socket.send('Hello World')
    };

    // alerts message pushed from server
    socket.onmessage = function(msg) {
        alert(JSON.stringify(msg));
    };

    // alert close event
    socket.onclose = function() {
        alert('closed');
    };

    // now, close the connection after 10 secons. (funny!)
    /*setInterval(function() {
        socket.close();
    }, 10000); */
</script>  

</html>

(function() {

    // window object
    var global = window;

    // WebSocket Object. All listener methods are cleaned up!
    var WebSocket = global.WebSocket = function(url) {
    alert("---")
        // get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
        this.socket = WebSocketFactory.getInstance(url);
        // store in registry
        if(this.socket) {
            WebSocket.store[this.socket.getId()] = this;
        } else {
            throw new Error('Websocket instantiation failed! Address might be wrong.');
        }
    };

    // storage to hold websocket object for later invokation of event methods
    WebSocket.store = {};

    // static event methods to call event methods on target websocket objects
    WebSocket.onmessage = function (evt) {
        WebSocket.store[evt._target]['onmessage'].call(global, evt);
    }   

    WebSocket.onopen = function (evt) {
        WebSocket.store[evt._target]['onopen'].call(global, evt);
    }

    WebSocket.onclose = function (evt) {
        WebSocket.store[evt._target]['onclose'].call(global, evt);
    }

    WebSocket.onerror = function (evt) {
        WebSocket.store[evt._target]['onerror'].call(global, evt);
    }

    // instance event methods
    WebSocket.prototype.send = function(data) {
        this.socket.send(data);
    }

    WebSocket.prototype.close = function() {
        this.socket.close();
    }

    WebSocket.prototype.getReadyState = function() {
        this.socket.getReadyState();
    }
    ///////////// Must be overloaded
    WebSocket.prototype.onopen = function(){
        throw new Error('onopen not implemented.');
    };

    // alerts message pushed from server
    WebSocket.prototype.onmessage = function(msg){
        throw new Error('onmessage not implemented.');
    };

    // alerts message pushed from server
    WebSocket.prototype.onerror = function(msg){
        throw new Error('onerror not implemented.');
    };

    // alert close event
    WebSocket.prototype.onclose = function(){
        throw new Error('onclose not implemented.');
    };
})();
4

1 回答 1

0

我将 socket.io 与 phonegap 一起使用。您可以将演示项目上传到 nodejitsu。

于 2013-06-24T04:45:04.760 回答