Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchat.js
More file actions
Latest commit
92 lines (79 loc) · 3.01 KB
/
Copy pathchat.js
File metadata and controls
92 lines (79 loc) · 3.01 KB
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
varexpress=require('express'),
app=express(),
http=require('http'),
server=http.createServer(app),
io=require('socket.io').listen(server);
server.listen(3000);
// routing
app.get('/',function(req,res){
res.sendFile(__dirname+'/index.html');
});
varusernames={};
varrooms=['room1','room2','room3'];
io.sockets.on('connection',function(socket){
//Everything we do in here is related to socket connections
//socket is the user, we can communicate with it from here.
socket.on('addUser',function(username){
//When the client emits an add user, we perform that functionality
socket.username=username;
console.log(username+"Connected");
//tell the user to join the default room
socket.room=rooms[0];
//save the user
usernames[username]=username;
//physically join the player and it's socket to the room
socket.join(socket.room);
//emit, to the client, you've connected
updateClient(socket,username,socket.room);
//emit to the room, a person has connected
//socket.broadcast.to(socket.room).emit('updateChat', 'SERVER', username + ' has connected');
updateChatRoom(socket,'connected');
updateRoomList(socket,socket.room);
});
//take in the message, emit it
socket.on('sendChat',function(data){
//send the message to everyone
console.log(socket.username+" sent a message");
io.sockets.in(socket.room).emit('updateChat',socket.username,data);
})
//when we switch a room
socket.on('switchRoom',function(newRoom){
socket.leave(socket.room);
socket.join(newRoom);
//update client
updateClient(socket,socket.username,newRoom);
//update old room
updateChatRoom(socket,'disconnected');
//change room
socket.room=newRoom;
//update new room
updateChatRoom(socket,'connected');
updateRoomList(socket,socket.room);
})
//disconnecting from a room
socket.on('diconnect',function(){
// remove the user from global list
deleteusernames[socket.username];
// tell the user list on the client side
io.sockets.emit('updateUsers',usernames);
//tell everyone\
updateGlobal(socket,'disonnected');
//leave channel
socket.leave(socket.room);
})
});
//update single client with this.
functionupdateClient(socket,username,newRoom){
socket.emit('updateChat','SERVER','You\'ve connected to '+newRoom);
}
functionupdateRoomList(socket,currentRoom){
socket.emit('updateRooms',rooms,currentRoom);
}
//We will use this function to update the chatroom when a user joins or leaves
functionupdateChatRoom(socket,message){
socket.broadcast.to(socket.room).emit('updateChat','SERVER',socket.username+' has '+message);
}
//We will use this function to update everyone!
functionupdateGlobal(socket,message){
socket.broadcast.emit('updateChat','SERVER',socket.username+' has '+message);
}