Skip to content

Commit

Permalink
versao 3.0 WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
José René Campanario committed Oct 7, 2019
1 parent 5ac2a56 commit 2005452
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ timesheet.mv.db
timesheet.trace.db
timesheet-old.mv.db
timesheet-copy.db
timesheet-copy.mv.db
2 changes: 1 addition & 1 deletion app.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}

.vis-nested-group.toBreak {
background-color: green;
background-color: lightgreen;
}

.vis-item.blue {
Expand Down
32 changes: 23 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var app = new Vue({
item.end = getMoment().hours(hora).minutes(minuto).seconds(0).milliseconds(0);
}
items.update(item);
persistItem(item, 'I');
});
this.preencherTimeSheet(event, idSubGroup);
},
Expand Down Expand Up @@ -257,6 +258,9 @@ function actionFired(properties) {
if (group.className != 'toBreak' && item.typeOfWork === 'Work' && diffMinutes >= 90) {
group.className = 'toBreak';
groups.update(group);
} else if (group.className === 'toBreak' && item.typeOfWork === 'Work' && diffMinutes < 90) {
group.className = 'p';
groups.update(group);
} else if (group.className === 'toBreak' && item.typeOfWork != 'Work') {
group.className = 'p';
groups.update(group);
Expand Down Expand Up @@ -485,7 +489,6 @@ function showGroupStatus() {
}
}
function fillSectors() {
console.log('Preenchendo Setores...');
var group = getGroupByName("Wipedown");
if (group.length == 0) {
var id = groups.add({ id: 'Wipedown', order: 0, content: 'Wipedown', nestedGroups: [] })[0];
Expand Down Expand Up @@ -543,10 +546,18 @@ function uuidv4() {
);
}

function updateDateItem(item) {
if (typeof item.start != 'undefined') {
item.start = moment(item.start);
}
if (typeof item.end != 'undefined') {
item.end = moment(item.end);
}
}

var socket;
if (window.WebSocket) {
var url = "ws://" + location.hostname + ":" + (parseInt(location.port) + 1) + "/";
console.log(url);
socket = new WebSocket(url);
socket.onmessage = function (event) {
var action = JSON.parse(event.data);
Expand All @@ -556,32 +567,35 @@ if (window.WebSocket) {
if (action.action === 'PERSIST') {
if (action.type === 'G') {
var g = groups.get(action.item.id);
updateDateItem(action.item);
if (g == null) {
groups.add(action.item);
} else {
groups.update(action.item);
}
} else if (action.type === 'I') {
var i = items.get(action.item.id);
if (group == null) {
i.add(action.item);
updateDateItem(action.item);
if (i == null) {
items.add(action.item);
} else {
i.update(action.item);
items.update(action.item);
}
}
} else if (action.action === 'REMOVE') {
if (action.type === 'G') {
var group = groups.get(action.item.id);
if (group == null) {
groups.remove(action.item);
if (group != null) {
groups.remove(group);
}
} else if (action.type === 'I') {
var i = items.get(action.item.id);
if (i == null) {
i.remove(action.item);
if (i != null) {
items.remove(i);
}
}
}
timeline.fit();
};
socket.onopen = function (event) {
};
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ <h5 class="modal-title">{{employeeName}}'s Timesheet</h5>
<button type="button" v-on:click="adicionar" class="btn btn-primary">Add</button>
</div>
<div class="col">
Versao 2.2 toBreak
Versao 3.0 WebSocket
</div>
</div>
<div>
Expand Down
12 changes: 10 additions & 2 deletions service/server/src/main/java/com/timesheet/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import org.h2.server.web.DbStarter;
Expand Down Expand Up @@ -63,15 +65,21 @@ private static void initializeServers() {
Undertow webSockerServer = Undertow.builder().addHttpListener(Integer.parseInt(SERVICE_PORT) + 1, SERVICE_BIND)
.setHandler(path().addPrefixPath("/",
new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {

Set<WebSocketChannel> channels = new HashSet<WebSocketChannel>();
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
channels.add(channel);
channel.getReceiveSetter().set(new AbstractReceiveListener() {

@Override
protected void onFullTextMessage(WebSocketChannel channel,
BufferedTextMessage message) {
WebSockets.sendText(message.getData(), channel, null);
String originalMessage = message.getData();
for (WebSocketChannel c : channels) {
if (!c.equals(channel)) {
WebSockets.sendText(originalMessage, c, null);
}
}
}
});
channel.resumeReceives();
Expand Down

0 comments on commit 2005452

Please sign in to comment.