Answer the question
In order to leave comments, you need to log in
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)
{
//Обработка события
}
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');
}
};
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question