mkuklis / phonegap-websocket

Websocket PhoneGap plugin for Android
203 stars 78 forks source link

If the server disconnected, how to re-connect? #48

Closed Netpolice closed 9 years ago

Netpolice commented 9 years ago

If the server disconnected, how to re-connect? If calling WebSocket("ws://xxx.xxxx.xxx.xx:8080"); again, the onopend not fired. Thanks.

Netpolice commented 9 years ago

I MADE IT.... in the phonegap-websocket.js

reconnect: function() {
        this.readyState = WebSocket.CONNECTING;
        cordova.exec(
          function (event) {
            socket._handleEvent(event);
          },
          function (event) {
            socket._handleEvent(event);
          }, "WebSocket", "connect", [ this.socketId, this.url, this.options ]);
    },

in the client js

            socket.onclose   = function(msg) { 
                if(connected == 1) {
                    connected = 0;
                    socket.reconnect();
                } else {
                    setTimeout(function() {
                        socket.reconnect();
                    }, 1500);
                }
           };
mkuklis commented 9 years ago

This looks like a good approach.