-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathapp.js
154 lines (118 loc) · 5.08 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* app.js
*
* Front-end code and event handling for sailsChat
*
*/
// Attach a listener which fires when a connection is established:
io.socket.on('connect', function socketConnected() {
// Show the main UI
$('#disconnect').hide();
$('#main').show();
// Announce that a new user is online--in this somewhat contrived example,
// this also causes the CREATION of the user, so each window/tab is a new user.
io.socket.get("/user/announce", function(data){
window.me = data;
updateMyName(data);
// Get the current list of users online. This will also subscribe us to
// update and destroy events for the individual users.
io.socket.get('/user', updateUserList);
// Get the current list of chat rooms. This will also subscribe us to
// update and destroy events for the individual rooms.
io.socket.get('/room', updateRoomList);
});
// Listen for the "room" event, which will be broadcast when something
// happens to a room we're subscribed to. See the "autosubscribe" attribute
// of the Room model to see which messages will be broadcast by default
// to subscribed sockets.
io.socket.on('room', function messageReceived(message) {
switch (message.verb) {
// Handle room creation
case 'created':
addRoom(message.data);
break;
// Handle a user joining a room
case 'addedTo':
// Post a message in the room
postStatusMessage('room-messages-'+message.id, $('#user-'+message.addedId).text()+' has joined');
// Update the room user count
increaseRoomCount(message.id);
break;
// Handle a user leaving a room
case 'removedFrom':
// Post a message in the room
postStatusMessage('room-messages-'+message.id, $('#user-'+message.removedId).text()+' has left');
// Update the room user count
decreaseRoomCount(message.id);
break;
// Handle a room being destroyed
case 'destroyed':
removeRoom(message.id);
break;
// Handle a public message in a room. Only sockets subscribed to the "message" context of a
// Room instance will get this message--see the "join" and "leave" methods of RoomController.js
// to see where a socket gets subscribed to a Room instance's "message" context.
case 'messaged':
receiveRoomMessage(message.data);
break;
default:
break;
}
});
// Listen for the "user" event, which will be broadcast when something
// happens to a user we're subscribed to. See the "autosubscribe" attribute
// of the User model to see which messages will be broadcast by default
// to subscribed sockets.
io.socket.on('user', function messageReceived(message) {
switch (message.verb) {
// Handle user creation
case 'created':
addUser(message.data);
break;
// Handle a user changing their name
case 'updated':
// Get the user's old name by finding the <option> in the list with their ID
// and getting its text.
var oldName = $('#user-'+message.id).text();
// Update the name in the user select list
$('#user-'+message.id).text(message.data.name);
// If we have a private convo with them, update the name there and post a status message in the chat.
if ($('#private-username-'+message.id).length) {
$('#private-username-'+message.id).html(message.data.name);
postStatusMessage('private-messages-'+message.id,oldName+' has changed their name to '+message.data.name);
}
break;
// Handle user destruction
case 'destroyed':
removeUser(message.id);
break;
// Handle private messages. Only sockets subscribed to the "message" context of a
// User instance will get this message--see the onConnect logic in config/sockets.js
// to see where a new user gets subscribed to their own "message" context
case 'messaged':
receivePrivateMessage(message.data);
break;
default:
break;
}
});
// Add a click handler for the "Update name" button, allowing the user to update their name.
// updateName() is defined in user.js.
$('#update-name').click(updateName);
// Add a click handler for the "Send private message" button
// startPrivateConversation() is defined in private_message.js.
$('#private-msg-button').click(startPrivateConversation);
// Add a click handler for the "Join room" button
// joinRoom() is defined in public_message.js.
$('#join-room').click(joinRoom);
// Add a click handler for the "New room" button
// newRoom() is defined in room.js.
$('#new-room').click(newRoom);
console.log('Socket is now connected!');
// When the socket disconnects, hide the UI until we reconnect.
io.socket.on('disconnect', function() {
// Hide the main UI
$('#main').hide();
$('#disconnect').show();
});
});