Commit 68f0d2b3 authored by edvard's avatar edvard

websockets full working

parent 73fa7e57
......@@ -38,9 +38,7 @@
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
......@@ -103,6 +101,11 @@
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
......
package cz.aos.service;
import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.OnClose;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
/**
*
* @author edvard
*/
@ServerEndpoint("/endpoint")
public class MyWSEndpoint {
static int count = 0;
@OnMessage
public String onMessage(String message) {
System.out.println("Server prijima zpravu: " + message);
if (message.equals("count")) {
return count + "";
} else {
return "Zprava prijata: " + message;
}
}
/**
* Otevreni noveho websocket spojeni.
*
* @param session
* @throws IOException
*/
@OnOpen
public void onOpen(Session session) throws IOException {
System.out.println("Novy uzivatel");
count++;
session.getBasicRemote().sendText(count+"");
}
@OnClose
public void onClose(Session s) {
count--;
System.out.println("WebSocket session ends");
}
}
......@@ -16,7 +16,7 @@
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/aos"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="mysiahoboje"/>
<property name="javax.persistence.jdbc.password" value="lopuch"/>
<property name="eclipseink.logging.level" value="INFO"/>
......
......@@ -35,6 +35,7 @@
</ul>
</nav>
<div id="content"></div>
<div id="onlineClients"></div>
</div>
<script src="/airline/jquery-2.1.1.min.js"></script>
<script src="/airline/script.js"></script>
......
pickerformat = "Y-m-d H:i:s";//-MM-DDThh:mm:ssTZD
headers = {};
......@@ -23,10 +22,12 @@ Client = (function () {
this.showFlightsList = __bind(this.showFlightsList, this);
this.showFlightForm = __bind(this.showFlightForm, this);
this.setUpListeners = __bind(this.setUpListeners, this);
}
Client.prototype.run = function () {
OnlineClients().start();
return this.setUpListeners();
};
......@@ -36,7 +37,7 @@ Client = (function () {
document.getElementById("makePayment").addEventListener('click', this.showPaymentForm, false);
document.getElementById("createDestination").addEventListener('click', this.showDestinationForm, false);
document.getElementById("listDestinations").addEventListener('click', this.showDestinationsList, false);
document.getElementById("createReservation").addEventListener('click', this.showReservationForm, false);
document.getElementById("createReservation").addEventListener('click', this.showReservationForm, false);
return document.getElementById("listReservations").addEventListener('click', this.showReservationsList, false);
};
......@@ -54,7 +55,7 @@ Client = (function () {
var form;
return form = new DestinationForm(this.content);
};
Client.prototype.showPaymentForm = function (e) {
var form;
return form = new PaymentForm(this.content);
......@@ -167,7 +168,7 @@ PaymentForm = (function () {
this.data[ar[0].name] = ar[0].value;
this.data["url"] = "";
$.ajax({
url: 'http://localhost:8080/airline/source/reservation/'+Number(ar[1].value)+'/payment',
url: 'http://localhost:8080/airline/source/reservation/' + Number(ar[1].value) + '/payment',
type: 'POST',
dataType: 'json',
contentType: 'application/json',
......@@ -339,7 +340,7 @@ FlightList = (function () {
headers["X-Base"] = $(this).val();
client.showFlightsList();
});
//razeni
$("th span").click(function () {
headers["X-Order"] = $(this).attr("data-value");
......@@ -568,7 +569,7 @@ ReservationList = (function () {
$.ajax({
url: 'http://localhost:8080/airline/source/reservation',
type: 'GET',
dataType: 'json'
dataType: 'json'
}).done(this.printList);
}
......@@ -628,7 +629,7 @@ ReservationList = (function () {
url: 'http://localhost:8080/airline/source/reservation/' + this.id,
type: 'GET',
dataType: 'json',
headers: { 'x-password': prompt("Password") }
headers: {'x-password': prompt("Password")}
}).done(this.createForm);
};
......@@ -664,7 +665,7 @@ ReservationList = (function () {
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(this.data),
headers: { 'x-password': prompt("Password") }
headers: {'x-password': prompt("Password")}
});
return $(this.content).empty();
};
......@@ -677,5 +678,26 @@ ReservationList = (function () {
})();
OnlineClients = function () {
start = function () {
ws = new WebSocket("ws://" + document.location.hostname + ":" + document.location.port + "/airline/endpoint");
setTimeout(function () {
ws.onmessage = function (evt) {
jQuery("#onlineClients").text("Počet klientů:" + evt.data);
};
}, 2000);
setInterval(repeat = function () {
ws.send("count");//zeptat se websocket na pocet klientu
}, 2000);
};
return {start: start};
};
client = new Client(document.getElementById("container"), document.getElementById("content"));
client.run();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment