在Android中集成Pusher和socket编程

时间:2013-06-27 11:08:20

标签: android cordova

我正在使用pusher实现套接字编程。但我不知道在哪里写ip地址以便我可以从服务器获取数据。 我正在使用这个例子 https://github.com/pusher/pusher-phonegap-android 你能告诉我在哪里写Ip s我会得到回应。 (函数($){

  var CONFIG = {
    PUSHER: {
      APP_KEY: '49e26cb8e9dde3dfc009'
    }
  };

  Pusher.log = function(msg) {
    if (window.console && window.console.log) {
      window.console.log(msg);
      $( '#debug_log' ).prepend( msg );
    }
  };

  document.addEventListener("deviceready", onDeviceReady, false);

  function onDeviceReady() {

    // Get device info
    var deviceInfo = 'Device Name: ' + device.name + '<br />' +
      'Device Cordova: ' + device.cordova + '<br />' +
      'Device Platform: ' + device.platform + '<br />' +
      'Device UUID: ' + device.uuid + '<br />' +
      'Device Version: ' + device.version + '<br />';

    $('#deviceProperties').html(deviceInfo)

    // Connect
    var pusher = new Pusher("ws://192.168.1.3:8101/");// i am writting Here my ip but connection problem occur
    pusher.connection.bind('state_change', connectionStateChange);

    function connectionStateChange(state) {
      $('#connectionStatus').html(state.current);
    }

    // Subscribe
    var channel = pusher.subscribe('my-channel');
    channel.bind('pusher:subscription_succeeded', subscriptionSucceeded);

    function subscriptionSucceeded() {
      $('#subscriptionStatus').html('succeeded');
    }

    channel.bind('my-event', handleMyEvent);

    function handleMyEvent(data) {
      $('#debug_log').append('<pre>' + JSON.stringify(data, null, 2) + '</pre>');
    }

  }

})(jQuery); 

1 个答案:

答案 0 :(得分:0)

我身边的愚蠢问题,是否有websocket服务器在指定的主机上监听?

更新。:Pusher是托管平台,因此您无法在本地运行它。如果您愿意,可以使用像Slanger(https://github.com/stevegraham/slanger)这样的开源替代品。请参阅它的文档,其中描述了如何相应地设置Pusher客户端。