PhoneGap - 套接字IO

时间:2015-01-21 12:48:37

标签: android ios sockets cordova socket.io

我正在使用Socket IO intregration处理PhoneGap应用程序。

DoTCP: function(driverid) {

  console.log("=== Opening up TCP ===");

  var Connections = 0;

  console.log("=== Cleaning between " + OpenConnections.length + "sockets that are disconnected ===");
  for (var i = 0; i < OpenConnections.length; i++) {
    if (OpenConnections[i] == null) {
      OpenConnections[i] = null;
    }
  };

  console.log("=== " + OpenConnections.length + " sockets up ===");

  if (OpenConnections.length > 0) {
    for (var i = 0; i < OpenConnections.length; i++) {
      if (OpenConnections[i].connected == true) {
        Connections++;
      }
    };
  }

  console.log("=== " + Connections + " socekts are connected ===");

  if (Connections == 0) {
    console.log("=== Creating a new socket ===");
    socket = io.connect('http://dispatch.pizza24.com:3000');
  }


  /**
   * Sends geo positions to the server every 60 seconds
   * @type {[type]}
   */
  if (TCPGeoPosInterval === false) {
    TCPGeoPosInterval = setInterval(function() {
      //if (socket.connected == true && GeoIntialized == true) {
      console.log("Sending postion... " + "P:" + Geo.Lat + ":" + Geo.Lon);
      socket.emit("data", "P:" + Geo.Lat + ":" + Geo.Lon);
      //}
    }, 60000);
  }


  /**
   * Send keep alive to the dispatch server
   * @type {[type]}
   */
  if (TCPInterval === false) {
    TCPInterval = setInterval(function() {
      //if (socket.connected == true) {
      console.log("Sending ping..." + "I:" + UserData.DriverID);
      socket.emit('data', "I:" + UserData.DriverID);
      //}
    }, 60000);
  }


  socket.on('connect', function() {

    OpenConnections.push(socket);

    console.log("=== Socket connection has been established ===");
    console.log("=== Sending data I:" + UserData.DriverID + " to verify for socket server ===");
    socket.emit('data', "I:" + UserData.DriverID);

    /**
     * Connection is open - light up the icon
     */
    $(".server-status img").attr({
      "src": "img/dot-circle-o_on.png"
    });


    socket.on('anything', function(data, callback) {
      console.log("========" + data + "========");
    });

    socket.on('reconnect', function() {

      OpenConnections.push(socket);

      console.log("=== Socket reconnection has been established ===");
      console.log("=== Sending data I:" + UserData.DriverID + " to verify for socket server ===");
      socket.emit('data', "I:" + UserData.DriverID);

      /**
       * Connection is open - light up the icon
       */
      $(".server-status img").attr({
        "src": "img/dot-circle-o_on.png"
      });

    });



    socket.on('data', function(text) {

      if (text == "B:1") {
        console.log("=== Incoming dispatch, checking for orders ===");
        P24.DoCheck(UserData.DriverID, UserData.Token, Geo.Lat, Geo.Lon);
      }

    });

    socket.on('disconnect', function() {
      console.log("=== Socket has been disconnected ===");
      var Conn = 0;
      socket = null;

      console.log("=== Cleaning between " + OpenConnections.length + " sockets that are disconnected ===");
      for (var i = 0; i < OpenConnections.length; i++) {
        if (OpenConnections[i] != null || OpenConnections[i].connected == true) {
          Conn++;
        }
      };

      console.log("=== " + OpenConnections.length + " sockets up ===");

      if (Conn === 0) {

        console.log("=== No socket connections, setting upp the TCPConnTimeout ===");

        if (TCPConnTries >= 6) {
          TCPConnTries = 0;
          P24.AlertBox(Locale.Texts.alerttitle, Locale.Texts.tcp_connection_timeout);
        } else {
          TCPConnTries++;
        }


        $(".server-status img").attr({
          "src": "img/dot-circle-o_off.png"
        });

        console.log("######  Lost connection to TCP server  #######");

        clearInterval(TCPGeoPosInterval);
        clearInterval(TCPInterval);

        TCPGeoPosInterval = false;
        TCPInterval = false;

        TCPConnTimeout = setTimeout(function() {

          P24.DoTCP(UserData.DriverID);

        }, 30000);
      }
    });
  });
}

我会设法与套接字服务器建立连接并发送第一个数据I:DriverID。但是一段时间后我再次ping通它会断开然后再次尝试连接,但是没有发送I:DriverID。需要发送I:DriverID以使服务器接受连接,如果尚未发送特定数据,则服务器将超时连接。

出于某种原因,此错误仅发生在Android设备上,它可以在iOS设备上一目了然。

0 个答案:

没有答案