Post Top Ad

Post Top Ad

Tuesday, 14 January 2020

how to connect socket in android

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.dfoe.frimaster.CheckoutActivity;
import com.dfoe.frimaster.DashBoard;
import com.dfoe.frimaster.DataBaseServices.MasterMetaData;
import com.dfoe.frimaster.FragmentForDashBorad.DineInFragment;
import com.dfoe.frimaster.FragmentForDashBorad.TakeAwayFragment;
import com.dfoe.frimaster.OrderMenuActivity;
import com.dfoe.frimaster.SeatViewLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

import static com.dfoe.frimaster.DataBaseServices.MasterMetaData.ResturantPayment.order_no;

public class SocketIoClass {

    public Socket client;
    public String mySocketId = "";


    //Latest Url    public String host = "https://www.example.com";



    public SocketIoClass() {
        client = connectToSignallingServer();
    }

    private Socket connectToSignallingServer() {
        Socket client = null;

        MessageHandler messageHandler = new MessageHandler();
        try {
            client = IO.socket(host);
            client.on("id", messageHandler.onId);
            client.on("createMessage", messageHandler.onMessage);
            client.on("updateChat", messageHandler.onUpdateChat);
            client.on("leave", messageHandler.onLeave);
            client.on("broadcast", messageHandler.onBroadcast);
            client.on("disconnect", messageHandler.onDisconnect);
            client.connect();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return client;
    }

    public void sendMessage(JSONObject payload)
            throws JSONException {
        try {
            client.emit("createMessage", payload);

        } catch (Exception e) {

        }
    }

    private class MessageHandler {


        private MessageHandler() {

        }

        public Emitter.Listener onMessage = new Emitter.Listener() {
            @Override            public void call(Object... args) {

                try {
                    JSONObject data = (JSONObject) args[0];
                    if (data.getString("type").equals("payments")) {
                        JSONArray array = data.getJSONObject("message").getJSONArray("payments");
                        long sucess = DashBoard.db.insertRestaurantPayment(array);
                        if (sucess == -1) {
                            if (CheckoutActivity.checkoutActivityTaskHandler != null) {
                                CheckoutActivity.checkoutActivityTaskHandler.sendEmptyMessage(CheckoutActivity.CheckoutActivityTaskHandler.PaymentNotDone);
                            }
                        } else {
                            if (CheckoutActivity.checkoutActivityTaskHandler != null) {
                                CheckoutActivity.checkoutActivityTaskHandler.sendEmptyMessage(CheckoutActivity.CheckoutActivityTaskHandler.PaymentDone);
                            }
                            if (DineInFragment.dineHandler != null) {
                                DineInFragment.dineHandler.sendEmptyMessage(DineInFragment.DineHandler.Upate_UI);
                            }
                            if (TakeAwayFragment.TakeAwayHandler != null) {
                                TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.Upate_UI);
                            }
                         /*   if (TakeAwayFragment.TakeAwayHandler != null) {                                TakeAwayFragment.TakeAwayHandler .sendEmptyMessage(TakeAwayFragment.TakeAwayHandler .Upate_UI_TAKEWAY);                            }*/
                            if (TakeAwayFragment.TakeAwayHandler != null) {
                                TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.ShowToastMessage);
                            }

                        }
                    } else if (data.getString("type").equals("order_status")) {
                        JSONObject object = data.getJSONObject("message");
                        if (object.has("order_id") && object.has("order_status")) {
                            int order_id = object.getInt("order_id");
                            int order_status = object.getInt("order_status");
                            long sucess = DashBoard.db.updateOrderServerStatus(order_id, order_status);
                            if (sucess == -1) {

                            } else {
                                if (DashBoard.dashBoardTaskHandler != null) {
                                    DashBoard.dashBoardTaskHandler.sendEmptyMessage(DashBoard.DashBoardTaskHandler.ShowToastMessage);
                                }

                                if (TakeAwayFragment.TakeAwayHandler != null) {
                                    TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.Upate_UI);
                                }

                            }
                        }
                    } else if (data.getString("type").equals("confirm_delivery")) {
                        JSONObject object = data.getJSONObject("message");
                        /*                 if(object.has("order_no") && object.has("confirm_delivery")) {*/                        if (object.has("order_no")) {
                            int orderno = object.getInt("order_no");
                            long sucess = DashBoard.db.updateOrderConfrmStatus(orderno);

                            if (sucess == -1) {

                            } else {
                                if (TakeAwayFragment.TakeAwayHandler != null) {
                                    Message msg = new Message();
                                    msg.what = TakeAwayFragment.TakeAwayHandler.TAKE_AWAY_CONFIRM_DELIVERY;
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("order_no", orderno);
                                    msg.setData(bundle);
                                    TakeAwayFragment.TakeAwayHandler.sendMessage(msg);
                                }
                            }
                        }
                    } else if (data.getString("type").equals("cancel_order")) {
                        JSONObject object = data.getJSONObject("message");
                        if (object.has("order_no")) {
                            int orderno = object.getInt("order_no");
                            long sucess = -1;
                            if (object.has("order_status") && object.getInt("order_status") == 3) {
                                sucess = DashBoard.db.updateCancelConfirmStatusTo3(orderno);
                            } else {
                                sucess = DashBoard.db.updateCancelOrder(orderno);
                            }

                            long sucess1 = DashBoard.db.updateseatCancelOrder(orderno);

                            /*   if (sucess == -1 *//*|| sucess1 == -1*//*) {*///                                if ((sucess == -1) || (sucess1 == -1)) {TODO  .. why this condition is needed...?                            if ((sucess == -1)/* || (sucess1 == -1)*/) {

                            } else {
                                if (DineInFragment.dineHandler != null) {
                                    DineInFragment.dineHandler.sendEmptyMessage(DineInFragment.DineHandler.Upate_UI);
                                }
                                if (TakeAwayFragment.TakeAwayHandler != null) {
                                    TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.Upate_UI_TAKEWAY_CONFIRM);
                                }


                            }
                        }
                    } else if (data.getString("type").equals("accept_takeaway_order")) {
                        JSONObject object = data.getJSONObject("message");
                        if (object.has("order_no")) {
                            int orderno = object.getInt("order_no");
                            long sucess = DashBoard.db.updateCancelOrderTakeway(orderno);

                            if (sucess == -1) {

                            } else {
                                if (TakeAwayFragment.TakeAwayHandler != null) {
//                                    TakeAwayFragment.TakeAwayHandler .sendEmptyMessage(TakeAwayFragment.TakeAwayHandler .ShowToastMessage);                                    TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.Upate_UI_TAKEWAY);
                                }
                            }
                        }
                    }



         /*           else  if(data.getString("type").equals("confirm_delivery")) {                        JSONObject object = data.getJSONObject("message");                        JSONArray array = object.getJSONObject("message").getJSONArray("confirm_delivery");                        long sucess = DashBoard.db.insertRestaurantOrder(array);                        if(sucess == 1)                        {                            if (DashBoard.dashBoardTaskHandler != null) {                                TakeAwayFragment.TakeAwayHandler .sendEmptyMessage(TakeAwayFragment.TakeAwayHandler .Upate_UI_TAKEWAY);                            }                            if (DashBoard.dashBoardTaskHandler != null) {                                TakeAwayFragment.TakeAwayHandler .sendEmptyMessage(TakeAwayFragment.TakeAwayHandler .Upate_UI_TAKEWAY_CONFIRM);                            }                        }

                    }*/

                    else if (data.getString("type").equals("restaurant_orders")) {
                        int id = data.getJSONObject("message").getJSONArray("orders").getJSONObject(0).getInt("id");
                        JSONArray array = data.getJSONObject("message").getJSONArray("orders");//                        int orderNo = data.getJSONObject("message").getInt("order_no");
                        String msg = data.getJSONObject("message").getString("message");
                        long sucess = -1, sucess_deleted_order = -1,sucess1 = -1;
                        if (data.getJSONObject("message").has("deleted_orders")) {
                            sucess_deleted_order = 0;
                            //   JSONArray deleted_orders =  data.getJSONObject("message").getJSONArray("deleted_orders");////                            if(deleted_orders != null )//                            {//                                if(deleted_orders.length() > 0)//                                {//                                    sucess_deleted_order = 0;//                                    sucess_deleted_order = DashBoard.db.deletedOrderFromDatabase(deleted_orders,orderNo);//                                }//                                else//                                {//                                    sucess_deleted_order = 0;//                                }//                            }//                            else//                            {//                                sucess_deleted_order = 0;//                            }                        } else {
                            sucess_deleted_order = 0;
                        }

                        sucess = DashBoard.db.insertRestaurantOrder(array);
                        JSONObject obj = new JSONObject();
                        obj.put("restaurant_id","");
                        obj.put("order_no",orderNo);
                        obj.put("type",1);
                        obj.put("message",msg);
                        sucess1 = DashBoard.db.insertRestorentMessages(obj);
                        if ((sucess == -1) || (sucess_deleted_order == -1) || (sucess1 == -1)) {
                            if (OrderMenuActivity.orderMenuHandler != null) {
                                OrderMenuActivity.orderMenuHandler.sendEmptyMessage(OrderMenuActivity.OrderMenuHandler.OrdernotUpdated);
                            }
                        } else {
                            if (OrderMenuActivity.orderMenuHandler != null) {
                                OrderMenuActivity.orderMenuHandler.sendEmptyMessage(OrderMenuActivity.OrderMenuHandler.OrderUpdated);
                            }
                            if (DineInFragment.dineHandler != null) {
                                DineInFragment.dineHandler.sendEmptyMessage(DineInFragment.DineHandler.Upate_UI);
                            }
                            if (TakeAwayFragment.TakeAwayHandler != null) {
                                TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.Upate_UI);
                            }
                            if (TakeAwayFragment.TakeAwayHandler != null) {
                                TakeAwayFragment.TakeAwayHandler.sendEmptyMessage(TakeAwayFragment.TakeAwayHandler.ShowToastMessage);
                            }
                     /*       if (TakeAwayFragment.TakeAwayHandler != null) {                                TakeAwayFragment.TakeAwayHandler .sendEmptyMessage(TakeAwayFragment.TakeAwayHandler .Upate_UI_TAKEWAY);                            }*/
                        }
                    }
                } catch (Exception e) {
                    Log.d("." +
                            "",e+"\n"+args[0].toString());
                    e.printStackTrace();
                }
            }
        };

        public Emitter.Listener onId = new Emitter.Listener() {
            @Override            public void call(Object... args) {
                String id = (String) args[0];
                // mListener.onStatusChanged("On Id called " + id);                mySocketId = id;
                JSONObject object = new JSONObject();
                try {
                    object.put("type", "insert_userid");
                    JSONObject messgae = new JSONObject();
                    messgae.put("SocketId", mySocketId);
                    messgae.put("user_id", DashBoard.user_id);
                    messgae.put("restaurant_id", DashBoard.restaurant_id);
                    object.put("message", messgae);
                    sendMessage(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };


        public Emitter.Listener onUpdateChat = new Emitter.Listener() {
            @Override            public void call(Object... args) {

                String id = (String) args[0];
                // myId = id;
            }

        };

        public Emitter.Listener onLeave = new Emitter.Listener() {
            @Override            public void call(Object... args) {

                JSONObject data = (JSONObject) args[0];

            }
        };
        public Emitter.Listener onBroadcast = new Emitter.Listener() {

            @Override            public void call(Object... args) {
                // TODO Auto-generated method stub                JSONObject data = (JSONObject) args[0];

            }
        };

        public Emitter.Listener onDisconnect = new Emitter.Listener() {

            @Override            public void call(Object... args) {
                Message msg = new Message();
            }
        };
    }

}

No comments:

Post a Comment