N
N
Nicholas Unknown2019-12-29 20:03:33
JavaScript
Nicholas Unknown, 2019-12-29 20:03:33

How to implement duplex communication between server in Java EE and client in Android?

Good day to all.

I am doing my thesis. The essence of the work is to implement something similar to normal messengers like Viber, or whatever it looks
like
:) sane.

I study the book Professional Java for Web applications.
there in the 10th chapter there is an example of a demonstration of the operation of duplex communication between a client and a server using sockets.

On the server side, a class TicTacToeServer.java is created in which all methods of working on the server side are written.

@ServerEndpoint("/ticTacToe/{gameId}/{username}")
public class TicTacToeServer
{
    @OnOpen
    public void onOpen(Session session, @PathParam("gameId") long gameId,
                       @PathParam("username") String username)
    {
       //Обработка события
        
    }

    @OnMessage
    public void onMessage(Session session, String message,
                          @PathParam("gameId") long gameId)
    {
        //Обработка события
    }

    @OnClose
    public void onClose(Session session, @PathParam("gameId") long gameId)
    {
        //Обработка события
    }


Further, from the client side, we have a regular JSP page in which a JS script is written.
In this script, in fact, all work with sockets is registered.

var move;
            $(document).ready(function() {
                var modalError = $("#modalError");
                var modalErrorBody = $("#modalErrorBody");
                var modalWaiting = $("#modalWaiting");
                var modalWaitingBody = $("#modalWaitingBody");
                var modalGameOver = $("#modalGameOver");
                var modalGameOverBody = $("#modalGameOverBody");
                var opponent = $("#opponent");
                var status = $("#status");
                var opponentUsername;
                var username = '<c:out value="${username}" />';
                var myTurn = false;

                $('.game-cell').addClass('span1');

                if(!("WebSocket" in window))
                {
                    modalErrorBody.text('WebSockets are not supported in this ' +
                            'browser. Try Internet Explorer 10 or the latest ' +
                            'versions of Mozilla Firefox or Google Chrome.');
                    modalError.modal('show');
                    return;
                }

                modalWaitingBody.text('Connecting to the server.');
                modalWaiting.modal({ keyboard: false, show: true });

                var server;
                try {
                    server = new WebSocket('ws://' + window.location.host +
                        '<c:url value="/ticTacToe/${gameId}/${username}"><c:param name="action" value="${action}" /></c:url>');
                } catch(error) {
                    modalWaiting.modal('hide');
                    modalErrorBody.text(error);
                    modalError.modal('show');
                    return;
                }

                server.onopen = function(event) {
                    //Обработка события
                };

                window.onbeforeunload = function() {
                    //Обработка события
                };

                server.onclose = function(event) {
                   //Обработка события
                };

                server.onerror = function(event) {
                    //Обработка события
                };

                server.onmessage = function(event) {
                          //Обработка события 
                };

                move = function(row, column) {
                    if(!myTurn) {
                        modalErrorBody.text('It is not your turn yet!');
                        modalError.modal('show');
                        return;
                    }
                    if(server != null) {
                        server.send(JSON.stringify({ row: row, column: column }));
                        $('#r' + row + 'c' + column).unbind('click')
                                .removeClass('game-cell-selectable')
                                .addClass('game-cell-player game-cell-taken');
                        toggleTurn(false);
                    } else {
                        modalErrorBody.text('Not connected to came server.');
                        modalError.modal('show');
                    }
                };
            });


Since my client will be on Android, I need to implement something on Android java that will be similar or better in functionality than in the above script.

The question is how is this implemented?

If you have more logical suggestions and (or) my idea is nonsense, then I will be glad to read your suggestions, since I climbed into the jungle in which I understand very little. To be honest, I don’t know how to write messengers at all and I’m trying to find something that will put me on the right track.

Thank you for your attention!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayton2019, 2020-12-30
@mayton2019

You are doing it right. Google for the keyword WebSockets. And all the frameworks that are connected with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question