Last active
July 9, 2018 18:20
-
-
Save mayuroks/e1d4afbdea3117fd4211443fbb38b3de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Implementation of {@link EventService} which connects and disconnects to the server. | |
| * It also sends and receives events from the server. | |
| */ | |
| public class EventServiceImpl implements EventService { | |
| private static final String TAG = EventServiceImpl.class.getSimpleName(); | |
| private static final String SOCKET_URL = "https://socket-io-chat.now.sh"; | |
| private static final String EVENT_CONNECT = Socket.EVENT_CONNECT; | |
| private static final String EVENT_DISCONNECT = Socket.EVENT_DISCONNECT; | |
| private static final String EVENT_NEW_MESSAGE = "new message"; | |
| private static EventService INSTANCE; | |
| private static EventListener mEventListener; | |
| private static Socket mSocket; | |
| private String mUsername; | |
| // Prevent direct instantiation | |
| private EventServiceImpl() {} | |
| /** | |
| * Returns single instance of this class, creating it if necessary. | |
| * | |
| * @return | |
| */ | |
| public static EventService getInstance() { | |
| if (INSTANCE == null) { | |
| INSTANCE = new EventServiceImpl(); | |
| } | |
| return INSTANCE; | |
| } | |
| /** | |
| * Connect to the server. | |
| * | |
| * @param username | |
| * @throws URISyntaxException | |
| */ | |
| @Override | |
| public void connect(String username) throws URISyntaxException { | |
| mUsername = username; | |
| mSocket = IO.socket(SOCKET_URL); | |
| // Register the incoming events and their listeners | |
| // on the socket. | |
| mSocket.on(EVENT_CONNECT, onConnect); | |
| mSocket.on(EVENT_DISCONNECT, onDisconnect); | |
| mSocket.on(EVENT_NEW_MESSAGE, onNewMessage); | |
| mSocket.connect(); | |
| } | |
| /** | |
| * Disconnect from the server. | |
| * | |
| */ | |
| @Override | |
| public void disconnect() { | |
| if (mSocket != null) mSocket.disconnect(); | |
| } | |
| /** | |
| * Send chat message to the server. | |
| * | |
| * @param chatMessage | |
| * @return | |
| */ | |
| @Override | |
| public Flowable<ChatMessage> sendMessage(@NonNull final ChatMessage chatMessage) { | |
| return Flowable.create(new FlowableOnSubscribe<ChatMessage>() { | |
| @Override | |
| public void subscribe(FlowableEmitter<ChatMessage> emitter) throws Exception { | |
| mSocket.emit(EVENT_NEW_MESSAGE, chatMessage.getMessage()); | |
| emitter.onNext(chatMessage); | |
| } | |
| }, BackpressureStrategy.BUFFER); | |
| } | |
| /** | |
| * Set eventListener. | |
| * | |
| * When server sends events to the socket, those events are passed to the | |
| * RemoteDataSource -> Repository -> Presenter -> View using EventListener. | |
| * | |
| * @param eventListener | |
| */ | |
| @Override | |
| public void setEventListener(EventListener eventListener) { | |
| mEventListener = eventListener; | |
| } | |
| // On connect listener | |
| private Emitter.Listener onConnect = new Emitter.Listener() { | |
| @Override | |
| public void call(Object... args) { | |
| Log.i(TAG, "call: onConnect"); | |
| mSocket.emit("add user", mUsername); | |
| if (mEventListener != null) mEventListener.onConnect(args); | |
| } | |
| }; | |
| // On disconnect listener | |
| private Emitter.Listener onDisconnect = new Emitter.Listener() { | |
| @Override | |
| public void call(Object... args) { | |
| Log.i(TAG, "call: onDisconnect"); | |
| if (mEventListener != null) mEventListener.onDisconnect(args); | |
| } | |
| }; | |
| // On new message listener | |
| private Emitter.Listener onNewMessage = new Emitter.Listener() { | |
| @Override | |
| public void call(final Object... args) { | |
| Log.i(TAG, "call: onNewMessage"); | |
| if (mEventListener != null) mEventListener.onNewMessage(args); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment