1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
var stompClient = null;
function setConnected(connected) {
$("#connect").prop("disabled", connected);
$("#disconnect").prop("disabled", !connected);
if (connected) {
$("#game-list").show();
} else {
$("#game-list").hide();
}
$("#greetings").html("");
}
function connect() {
var socket = new SockJS('/seven-wonders-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/user/queue/errors', function (msg) {
var error = JSON.parse(msg.body);
console.error(error);
});
stompClient.subscribe('/topic/games', function (msg) {
var games = JSON.parse(msg.body);
console.log("Received new games: " + games);
for (var i = 0; i < games.length; i++) {
addNewGame(games[i]);
}
});
stompClient.subscribe('/user/queue/join-game', function (msg) {
var game = JSON.parse(msg.body);
console.log("Joined game: " + game);
addNewPlayer(game);
});
});
}
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}
function sendCreateGame(gameName, playerName) {
stompClient.send("/app/lobby/create-game", {}, JSON.stringify({
'gameName': gameName,
'playerName': playerName
}));
}
function sendJoinGame(gameName, playerName) {
stompClient.send("/app/lobby/join-game", {}, JSON.stringify({
'gameName': gameName,
'playerName': playerName
}));
}
function addNewGame(game) {
console.log(game);
$("#game-list-content").append('<tr><td>' + game.name + '</td><td><button id="join-' + game.id +
'" type="submit">Join</button></td></tr>');
$("#join-" + game.id).click(function () {
sendJoinGame(game.name, $("#player-name-field").val());
});
}
function addNewPlayer(player) {
console.log(player);
}
$(function () {
$("form").on('submit', function (e) {
e.preventDefault();
});
$("#connect").click(function () {
connect();
});
$("#disconnect").click(function () {
disconnect();
});
$("#create-game").click(function () {
sendCreateGame($("#game-name-field").val(), $("#player-name-field").val());
});
});
|