如何在android中实现即时消息应用

如何在android中实现即时消息应用,android,socket.io,instant-messaging,Android,Socket.io,Instant Messaging,我正在开发一个即时消息android应用程序。我已经成功地实现了我的应用程序和web应用程序之间的聊天。我已经使用编译'io.socket:socket.io客户端:0.6.2'库来实现这一点。但是,当我在另一部手机上安装相同的应用程序时,设备间的通信无法工作。我想我错过了一些东西。我现在必须在我的应用程序中实现什么。广播接收器或GCM(谷歌云消息)。我的代码如下: 1。ChatActivity.java public class ChatActivity extends Activity {

我正在开发一个即时消息android应用程序。我已经成功地实现了我的应用程序和web应用程序之间的聊天。我已经使用
编译'io.socket:socket.io客户端:0.6.2'
库来实现这一点。但是,当我在另一部手机上安装相同的应用程序时,设备间的通信无法工作。我想我错过了一些东西。我现在必须在我的应用程序中实现什么。广播接收器或GCM(谷歌云消息)。我的代码如下:

1。ChatActivity.java

public class ChatActivity extends Activity {
public static final String TAG = ChatActivity.class.getSimpleName();
EditText edMessage;
Button sendMessage;
private Socket mSocket;
String sID, lID, md5StringRoomID, message, friendName, loggedInUser;
String frndID;
int smallerID, largerID;
AlmaChatDatabase almaChatDatabase;
// Chat messages list adapter
private MessagesListAdapter adapter;
private List<Message> listMessages;
private ListView listViewMessages;
boolean isSelf = false; // to check whether the message is owned by you or not.true means message is owned by you .
Message msg;
int loggedInUserID;
private String URL_FEED_Message = "";
APIConfiguration apiConfiguration;

{
    try {
        mSocket = IO.socket(Constants.CHAT_SERVER_URL);
        Log.e("Socket", String.valueOf(mSocket));
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    sendMessage = (Button) findViewById(R.id.btnSendMessage);
    almaChatDatabase = new AlmaChatDatabase(getApplicationContext());
    loggedInUserID = almaChatDatabase.getUserID(); // Getting ID of the Logged in user from the database
    Log.e("UserID", "Id of Logged in user  " + loggedInUserID);

    listMessages = new ArrayList<Message>();
    adapter = new MessagesListAdapter(getApplicationContext(), listMessages);
    listViewMessages = (ListView) findViewById(R.id.list_view_messages);
    listViewMessages.setAdapter(adapter);

    // Getting the ID of the friend from the previous screen using getExtras
    Bundle bundle = getIntent().getExtras();
    frndID = bundle.getString("ID");
    Log.e("FriendID", frndID);
    final int friendID = Integer.parseInt(frndID);
    friendName = bundle.getString("name");
    Log.e("FriendName", friendName);
    loggedInUser = almaChatDatabase.getUserName(); // Name of logged in user
    Log.e("LoggedInUser", loggedInUser);
    // Converting first lowercase letter of every word in Uppercase
    final String loggedInUpper = upperCase(loggedInUser);
    //To find the current time
    Date d = new Date();
    final long time = d.getTime();
    // Comparing the loggedInUserId and friendID
    if (friendID < loggedInUserID) {
        smallerID = friendID;
        largerID = loggedInUserID;
    } else {
        smallerID = loggedInUserID;
        largerID = friendID;
    }
    sID = String.valueOf(smallerID);
    lID = String.valueOf(largerID);
    String combinedID = sID + lID;
    Log.e("combined ID", combinedID);
    md5StringRoomID = convertPassMd5(combinedID); // Encrypting the combinedID to generate Room ID
    Log.e("md5StringRoomID", md5StringRoomID);

    // Using the API for loading old chat messages
    apiConfiguration = new APIConfiguration();
    String api_message = apiConfiguration.getApi_message(); // Getting the API of messages
    URL_FEED_Message = api_message + md5StringRoomID; // md5String is the encrypted room ID here
    Log.e("URL_FEED_MESSAGE", URL_FEED_Message);

    Log.e("Network request", "Fresh Request");
    // We first check for cached request
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(URL_FEED_Message);
    if (entry != null) {
        // fetch the data from cache
        try {
            String data = new String(entry.data, "UTF-8");
            try {
                parseJsonFeed(new JSONArray(data));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    } else {

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_FEED_Message, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray jsonArray) {
                Log.e("JsonArray", String.valueOf(jsonArray));
                if (jsonArray != null) {
                    parseJsonFeed(jsonArray);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("ErrorResponse", String.valueOf(volleyError));
            }
        }
        );
        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
    }


    edMessage = (EditText) findViewById(R.id.edtMessage);
    //Listening on Events
    mSocket.on(Socket.EVENT_CONNECT, onConnect);
    mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectionError);
    mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
    mSocket.on("send:notice", onReceive); // Listening event for receiving messages
    mSocket.connect(); // Explicitly call connect method to establish connection here
    mSocket.emit("subscribe", md5StringRoomID);

    sendMessage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //mSocket.emit("subscribe", md5String);
            message = edMessage.getText().toString().trim();
            Log.e("message", message);
            if (!message.equals("")) {
                edMessage.setText(" ");
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("room_id", md5StringRoomID);
                    jsonObject.put("user", loggedInUpper);
                    jsonObject.put("id", friendID);
                    jsonObject.put("message", message);
                    jsonObject.put("date", time);
                    jsonObject.put("status", "sent");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                isSelf = true; // Boolean isSelf is set to be true as sender of the message is logged in user i.e. you
                attemptToSend(loggedInUpper, message, isSelf);
                mSocket.emit("send", jsonObject); // owner i.e LoggedIn user is sending the message
            } else {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Please enter some text", Toast.LENGTH_LONG).show();

                    }
                });
            }
        }

    });

}

//Adding message in the arrayList
public void attemptToSend(String senderName, String message, boolean isSelf) {
    msg = new Message(senderName, message, isSelf);
    listMessages.add(msg);
    adapter.notifyDataSetChanged();
    playBeep();
}

// Playing sound when the message is sent by the owner
public void playBeep() {
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// encrypting string into MD5
public static String convertPassMd5(String pass) {
    String password = null;
    MessageDigest mdEnc;
    try {
        mdEnc = MessageDigest.getInstance("MD5");
        mdEnc.update(pass.getBytes(), 0, pass.length());
        pass = new BigInteger(1, mdEnc.digest()).toString(16);
        while (pass.length() < 32) {
            pass = "0" + pass;
        }
        password = pass;
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    }
    return password;
}

// Converting first lowercase letter of every word in Uppercase
String upperCase(String source) {
    StringBuffer res = new StringBuffer();
    String[] strArr = source.split(" ");
    for (String str : strArr) {
        char[] stringArray = str.trim().toCharArray();
        stringArray[0] = Character.toUpperCase(stringArray[0]);
        str = new String(stringArray);
        res.append(str).append(" ");
    }
    return res.toString().trim();
}

// Event Listeners
private Emitter.Listener onConnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("Socket", "Connected");
    }
};


private Emitter.Listener onConnectionError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("Error", "Error in connecting server");
    }
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("Disconnect", "Socket Disconnected");
    }
};

// Event Listener for receiving messages
private Emitter.Listener onReceive = new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        Log.e("Receive", "Message received");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                JSONObject data = (JSONObject) args[0];
                Log.e("DATA", String.valueOf(data));
                try {
                    JSONArray ops = data.getJSONArray("ops");
                    for (int i = 0; i < ops.length(); i++) {
                        JSONObject object = ops.getJSONObject(i);
                        String roomID = object.getString("room_id");
                        Log.e("RoomID", roomID); // Getting room ID from JSON array
                        Log.e("Md5RoomID", md5StringRoomID); // Getting room id which we have created using logged in user ID and room id of the user through which chat has to be done
                        //Comparing the room IDs
                        if (md5StringRoomID.equals(roomID)) {
                            String senderName = object.getString("user");
                            Log.e("Sender Name", senderName);
                            String senderID = object.getString("id");
                            Log.e("SenderID", senderID);
                            JSONObject message = object.getJSONObject("message");
                            String messageReceived = message.getString("text");
                            Log.e("Message Received", messageReceived);
                            String loggedInUSerNAme = almaChatDatabase.getUserName();
                            //If the message is sent by the owner to other from webapp ,then we need to check whether the sender is the loggedinUSer in the App or not and we will right align the messages .
                            if (loggedInUSerNAme.equalsIgnoreCase(senderName)) {
                                isSelf = true;
                                msg = new Message(senderName, messageReceived, isSelf);
                                listMessages.add(msg);
                                adapter.notifyDataSetChanged();
                                playBeep();
                            } else {
                                isSelf = false;
                                msg = new Message(senderName, messageReceived, isSelf);
                                listMessages.add(msg);
                                adapter.notifyDataSetChanged();
                                playBeep();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // Playing sound when the message is sent by other
    public void playBeep() {
        try {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

};

// Parsing JSon Array which corresponds to the old chat messages
public void parseJsonFeed(JSONArray jsonArray) {
    for (int i = 0; i < jsonArray.length(); i++) {
        try {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String roomID = jsonObject.getString("room_id");
            Log.e("RoomID", roomID);
            Log.e("Md5RoomID", md5StringRoomID);
            // If Room ID(created using id of logged in user and id of friend) matches with the room id obtained from JSON String
            if (md5StringRoomID.equals(roomID)) {
                String userName = jsonObject.getString("user");
                Log.e("Name", userName);
                String loggedInUSerNAme = almaChatDatabase.getUserName();
                Log.e("LoggedInUSer", loggedInUSerNAme);
                //If the message is sent by the owner to other from webapp ,then we need to check whether the sender is the loggedinUSer in the App or not and we will right align the messages .
                if (loggedInUSerNAme.equalsIgnoreCase(userName)) {
                    String message = jsonObject.getString("message");
                    Log.e("message", message);
                    isSelf = true;
                    msg = new Message(userName, message, isSelf);
                    listMessages.add(msg);
                    adapter.notifyDataSetChanged();
                    //playBeep();
                } else {
                    JSONObject jsonMessage = jsonObject.getJSONObject("message");
                    String message = jsonMessage.getString("text");
                    isSelf = false;
                    msg = new Message(userName, message, isSelf);
                    listMessages.add(msg);
                    adapter.notifyDataSetChanged();
                    // playBeep();
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        // notify data changes to list adapter
        //adapter.notifyDataSetChanged();
    }
}
public class Constants {

public static final String CHAT_SERVER_URL = "http://192.168.2.250:3000/";
}


除了设备对设备的通信,其他一切都正常工作。我可以从我的应用程序向web应用程序发送消息,也可以从web应用程序接收消息。但设备间的通信不起作用。请帮助我解决此问题。

Constants.CHAT\u SERVER\u URL设置为什么?我猜这是一个客户机/服务器拓扑?在设备/设备通信期间,您能否在服务器上看到来自双方的消息?您可以发布一个从上面代码输出的调试信息中的日志示例吗?顺便说一句,您在代码中只使用Log.e,这是针对错误的。您可以/应该使用Log.i获取信息output@LintfordPickle:我正在使用API加载旧消息。所有消息都存储在服务器上。例如,如果用户a向用户B发送消息“Hello”,它将显示在用户A的android应用程序和web应用程序中,但不会显示在用户B的android应用程序中。这是主要问题。您是否设法解决了此问题?你在使用GCM吗?@JaniBela:是的,我已经解决了这个问题。我在使用socket.io-client库。我没有使用GCM。Constants.CHAT\u SERVER\u URL设置为什么?我猜这是一个客户机/服务器拓扑?在设备/设备通信期间,您能否在服务器上看到来自双方的消息?您可以发布一个从上面代码输出的调试信息中的日志示例吗?顺便说一句,您在代码中只使用Log.e,这是针对错误的。您可以/应该使用Log.i获取信息output@LintfordPickle:我正在使用API加载旧消息。所有消息都存储在服务器上。例如,如果用户a向用户B发送消息“Hello”,它将显示在用户A的android应用程序和web应用程序中,但不会显示在用户B的android应用程序中。这是主要问题。您是否设法解决了此问题?你在使用GCM吗?@JaniBela:是的,我已经解决了这个问题。我在使用socket.io-client库。我没有使用GCM。