使用套接字从服务器获取实时数据

时间:2019-06-28 09:08:28

标签: java android sockets serversocket

我正在尝试获取来自服务器的实时数据,该服务器具有URL function paypalpayment() { global $wpdb; $user = wp_get_current_user(); $user_ID = $user->ID; $shortcode = $wpdb->get_row("SELECT MAX(ap.pending) AS pending, ap.book_datetime, ap.id, ap.hash FROM ea_appointments AS ap " ."INNER JOIN ea_users AS us ON ap.id_users_customer = us.id " ."WHERE us.wp_id ='".$user_ID."'"); $html = ''; if ($shortcode->pending == ''){ $html .= '<h1>Processing Error: Appointment has been deleted. </h1>'; $html .= '<p align="center"><a class="fep-button" style="width: 195px; text-align: center;" href="https://lectiotutoring.co.za/EasyBlue" /> Schedule an appointment</a></p>'; } else { $html .= '<h2>Fee Policy</h2>'; $html .= '<p>You may reschedule an appointment without charge within 24 hours of your appointment. Cancelation of an appointment within 24 hours can either result in a refund or a credit for your next appointment per your request. You will need to inform Lectio on the discussion board about how you would like your cancelation to be handled. If you would like a refund, refunds will be the full amount of the cost of your session minus the PayPal processing fees. There are no refunds for cancelations later than 24 hours in advance. <span class="bigger"><b>If payment is not completed within 10 minutes the appointment will be deleted.</b></span></p>'; $html .= '<meta http-equiv="refresh" content="'; $html .= $refreshtime; $html .= '">'; $html .= '<style> </style>'; $html .= '<input type="button" id="stepone" onclick="processpaypal()" value="Process Payment">'; $html .= '<div id="spinner" class="loader" style="display:none"></div>'; $html .= do_shortcode($shortcode->pending); $html .= '<input type="button" onclick="deletapt()" value="Delete Apt.">'; $html .= '<script> cancelurl = "https://lectiotutoring.co.za/EasyBlue/index.php/appointments/cancel/' . $shortcode->hash . '"; function deletapt(){ window.location = cancelurl; } $(document).ready(function() { $("input[name=return]").val("https://lectiotutoring.co.za/payment-success/"); $("input[name=cancel_return]").val(cancelurl); }); function processpaypal(){ $("#spinner").css("display","block"); $("#stepone").css("display","none"); setTimeout( function() { $(".wpi_checkout_submit_btn").click(); }, 250); } </script>'; } return $html; } add_shortcode("paypalpay", "paypalpayment"); //PayPal Callback function that replaces the cryptic suffix with actual service names in patients the WP-Invoice file. function ea_paypalcallback($transaction_data){ global $wpdb; //Grab Service Name and Date $session_id = $transaction_data[post_data][created_by][0]; $posturl = $transaction_data[post_data][guid][0]; $urlArray = explode('=',$posturl); $postid = $urlArray[sizeof($urlArray)-1]; $dummyname = $transaction_data[items][1][name]; $query = "SELECT concat(eas.name, ' ', DATE_FORMAT(eaa.start_datetime, '%m/%d/%Y') ) FROM ea_services AS eas " . "LEFT JOIN ea_appointments AS eaa ON eaa.id_services = eas.id WHERE eaa.pending LIKE '%" . $session_id . "%'"; $servicename = $wpdb->get_var($query); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_content = REPLACE(post_content, 'Items: $dummyname', '$dummyname: $servicename') WHERE ID = $postid")); $wpdb->query($wpdb->prepare("UPDATE $wpdb->postmeta SET meta_value = REPLACE(meta_value, '$session_id', '') WHERE meta_key = 'post_title' AND post_id = $postid")); sleep(5); $wpdb->query($wpdb->prepare("UPDATE ea_appointments SET pending = '' WHERE pending LIKE '%$session_id%'")); }

我不知道如何将其连接到服务器并从中获得数据

public class A
{
  // ...
}

public class B<T> : A
{
  // ...
}

public class Program
{
  public static A MakeA() { return new A(); }

  public static A MakeB() { return new B<string>(); }

  public static void Visit<T>(B<T> b)
  {
    Console.WriteLine("This is B with type "+typeof(T).FullName);
  }

  public static void Visit(A a)
  {
    Console.WriteLine("This is A");
  }

  public static void Main()
  {
    A instA = MakeA();
    A instB = MakeB();

    // This calls the appropriate methods.
    Visit((dynamic)instA);
    Visit((dynamic)instB);

    // This calls Visit(A a) twice.
    Visit(instA);
    Visit(instB);
  }
}

1 个答案:

答案 0 :(得分:0)

首先将此行添加到gradle

implementation 'com.neovisionaries:nv-websocket-client:2.4'

实现后,您必须创建一个类WebsocketClient才能连接并从服务器获取实时数据。

    public class WebsocketClient {

    public static String url = "ws://XXX.XXX.XXX/XXX";
    private static WebSocketClient mWebSocketClient;

    public static void connect_to_server(Context con) {


        URI uri;
        try {
            uri = new URI("ws://XXX.XXX.XXX/XXX");
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }

        mWebSocketClient = new WebSocketClient(uri, new Draft_17()) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Log.e("Websocket", "Opened");

            }

            @Override
            public void onMessage(String s) {

                Log.e("OnMessageRecieved>>>",s.toString());

            }

            @Override
            public void onClose(int code, String s, boolean b) {
                Log.e("Websocket", code + ": Closed " + s);
            }

            @Override
            public void onError(Exception e) {
                Log.e("Websocket", "Error " + e.getMessage());
            }
        };

        if(mWebSocketClient.getConnection().isConnecting()){

        }else{
            mWebSocketClient.connect();
        }
    }

}

并通过活动

连接到它
WebsocketClient.connect_to_server(MainAtivity.this)