Last active
August 5, 2020 15:03
-
-
Save Elblassy/a4c7c8d186f48851de8925c9a6ba6ba5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Socket mSocket; | |
{ | |
try { | |
mSocket = IO.socket("yourLinkToSocket"); | |
Log.d(TAG, "instance initializer: correct"); | |
} catch (URISyntaxException e) { | |
Log.d(TAG, "instance initializer: " + e.getMessage()); | |
} | |
} | |
private Emitter.Listener onNewMessage = new Emitter.Listener() { | |
@Override | |
public void call(final Object... args) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
if (dialog.isShowing()) { | |
dialog.dismiss(); | |
} | |
JSONObject data = (JSONObject) args[0]; | |
String username = ""; | |
String message = ""; | |
String role = ""; | |
try { | |
username = data.getString("user"); | |
message = data.getString("message"); | |
role = data.getString("role"); | |
} catch (JSONException e) { | |
Log.d(TAG, "message: error" + e.getMessage()); | |
} | |
chatModelList.add(new ChatModel(message, "", username, role)); | |
chatAdapter.notifyDataSetChanged(); | |
binding.layout.recyclerView.smoothScrollToPosition(chatModelList.size() - 1); | |
} | |
}); | |
} | |
}; | |
/* | |
* call this function in onCreate | |
*/ | |
private void initSocket() { | |
mSocket.on(Socket.EVENT_CONNECT, args -> { | |
mSocket.emit("userJoined", pref.getUserId(), pref.getSessionValue("firstName") + " " | |
+ pref.getSessionValue("lastName"), getIntent().getStringExtra("appEndTime")) | |
.emit("joinUserToRoom", roomId); | |
}).on("isTyping", new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
runOnUiThread(() -> { | |
if (args[0].equals("")) { | |
binding.toolbarTitle.setText(userName); | |
} else { | |
binding.toolbarTitle.setText(args[0].toString()); | |
} | |
}); | |
} | |
}).on("message", onNewMessage).on("time", args -> { | |
runOnUiThread(() -> { | |
binding.timer.setVisibility(View.VISIBLE); | |
long minutes = (Integer.parseInt(args[0].toString()) / 1000) / 60; | |
int seconds = ((Integer.parseInt(args[0].toString()) / 1000) % 60); | |
binding.timer.setText(String.valueOf(minutes + ":" + seconds)); | |
timer = binding.timer.getText().toString(); | |
}); | |
}).on("imageUploaded", onNewImage).on(Socket.EVENT_DISCONNECT, args -> { | |
runOnUiThread(() -> { | |
if (timer.equals("0:0")) { | |
Toast.makeText(this, "Your time is finished", Toast.LENGTH_LONG).show(); | |
} else { | |
Toast.makeText(this, "Your internet is disconnected", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
}); | |
mSocket.connect(); | |
} | |
/* | |
*to send message put in clickListener of Button | |
*/ | |
if (mSocket.connected()) { | |
Log.d(TAG, "onCreate: " + " connected"); | |
String message = binding.editText.getText().toString(); | |
mSocket.emit("sendMessage", message); | |
Log.d(TAG, "onCreate: " + message); | |
binding.editText.setText(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment