-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
77 lines (65 loc) · 1.68 KB
/
client.js
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
var io = require('socket.io');
var socket;
var listUri = '/list';
var listId;
function initSocket() {
socket = io.connect();
socket.on('connect', function() {
socket.emit('connectToList', listId);
});
socket.on('add', function(data) {
// Add item to list
console.log('Add ' + data.item + ' to list');
});
socket.on('remove', function(data) {
// Remove item from list
console.log('Remove ' + data.item + ' from list');
});
socket.on('message', function(message) {
console.log('Got message: ' + message);
});
}
function getListId() {
$.post(listUri, function(data) {
console.log("New list created: "+data);
localStorage.listId = data;
});
}
function getList() {
var url = listUri + '/' + localStorage.listId;
$.getJSON(
url,
function(items) {
// Show list in GUI
console.log('Got list from server');
}
);
}
function addItem(item) {
var url = listUri + '/' + localStorage.listId;
$.post(
url,
{ "item": item },
addItemToGUI(item)
);
}
function addItemToGUI(item) {
// Add item to GUI
console.log('Added ' + item + ' to GUI');
}
function removeItem(item) {
var url = listUri + '/' + localStorage.listId + '/' + item;
$.ajax({
type: 'DELETE',
url: url,
success: removeItemFromGUI(item)
});
}
function removeItemFromGUI(item) {
// Remove item from GUI list
console.log('Removed ' + item + ' from GUI');
}
function connectToList(listId) {
socket.emit('connectToList', listId);
console.log('Connected to list ' + listId);
}