Skip to content

Commit b99289d

Browse files
committed
Initial commit
0 parents  commit b99289d

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.html

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<script src="/socket.io/socket.io.js"></script>
2+
<script src="/js/jquery.min.js"></script>
3+
<script>
4+
var socket = io.connect('http://localhost:8080');
5+
6+
// on connection to server, ask for user's name with an anonymous callback
7+
socket.on('connect', function(){
8+
// call the server-side function 'adduser' and send one parameter (value of prompt)
9+
var name = prompt("What's your name?");
10+
var chatroom = prompt("What chat room?");
11+
if (name !== null && chatroom !== null)
12+
{
13+
socket.emit('adduser', {username: name, chatroom: chatroom});
14+
}
15+
});
16+
17+
socket.on('errorConnect', function(error) {
18+
alert(error);
19+
var name = prompt("What's your name?");
20+
var chatroom = prompt("What chat room?");
21+
socket.emit('adduser', {username: name, chatroom: chatroom});
22+
});
23+
24+
socket.on('successfullyjoinchat', function(chatroom) {
25+
$('#roomname').append(chatroom);
26+
});
27+
28+
// listener, whenever the server emits 'updatechat', this updates the chat body
29+
socket.on('updatechat', function (username, data) {
30+
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
31+
});
32+
33+
// listener, whenever the server emits 'updateusers', this updates the username list
34+
socket.on('updateusers', function(data) {
35+
$('#users').empty();
36+
$.each(data, function(key, value) {
37+
$('#users').append('<div>' + data[key] + '</div>');
38+
});
39+
});
40+
41+
// on load of page
42+
$(function(){
43+
// when the client clicks SEND
44+
$('#datasend').click( function() {
45+
var message = $('#data').val();
46+
$('#data').val('');
47+
// tell server to execute 'sendchat' and send along one parameter
48+
socket.emit('sendchat', message);
49+
});
50+
51+
// when the client hits ENTER on their keyboard
52+
$('#data').keypress(function(e) {
53+
if(e.which == 13) {
54+
$(this).blur();
55+
$('#datasend').focus().click();
56+
}
57+
});
58+
});
59+
60+
</script>
61+
<div>
62+
<b> Chatroom: </b>
63+
<span id="roomname"></span>
64+
</div>
65+
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
66+
<b>USERS</b>
67+
<div id="users"></div>
68+
</div>
69+
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
70+
<div id="conversation"></div>
71+
<input id="data" style="width:200px;" />
72+
<input type="button" id="datasend" value="send" />
73+
</div>

js/jquery.min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var express = require('express'),
2+
http = require('http');
3+
4+
var app = express();
5+
var server = http.createServer(app);
6+
var io = require('socket.io').listen(server, {log: false});
7+
8+
app.use("/js", express.static(__dirname + '/js'));
9+
10+
server.listen(8080);
11+
12+
// routing
13+
app.get('/', function (req, res) {
14+
res.sendfile(__dirname + '/index.html');
15+
});
16+
17+
// usernames which are currently connected to the chat
18+
var usernames = {};
19+
var chatrooms = {};
20+
io.sockets.on('connection', function(socket) {
21+
22+
socket.on('sendchat', function(data) {
23+
// we tell the client to execute 'updatechat with 2 parameters'
24+
io.sockets.in(socket.chatroom).emit('updatechat', socket.username, data);
25+
});
26+
27+
// when the client emits 'adduser', this listens and executes
28+
socket.on('adduser', function(info) {
29+
username = info.username;
30+
chatroom = info.chatroom;
31+
32+
if (!(usernames[username] == undefined || usernames[username] == null))
33+
{
34+
socket.emit('errorConnect', "The username '" + username + "' is already in use.");
35+
return;
36+
}
37+
// we store the username in the socket session for this client
38+
socket.username = username;
39+
// we store the room
40+
socket.chatroom = chatroom;
41+
if (chatrooms[chatroom] == undefined)
42+
{
43+
chatrooms[chatroom] = {
44+
name: chatroom,
45+
usernames: {},
46+
usercount: 0
47+
}
48+
}
49+
// add the client's username to the global list
50+
usernames[username] = username;
51+
chatrooms[chatroom].usernames[username] = username;
52+
chatrooms[chatroom].usercount++;
53+
socket.join(chatroom);
54+
55+
// echo to the client the chatroom name
56+
socket.emit('successfullyjoinchat', chatroom);
57+
// echo to the client they've connected
58+
socket.emit('updatechat', 'SERVER', 'you have connected');
59+
// echo globablly (all clients) that a person has connected
60+
socket.broadcast.to(chatroom).emit('updatechat', 'SERVER', username + ' has connected');
61+
// update the list of users in chat, client-side
62+
io.sockets.in(chatroom).emit('updateusers', chatrooms[chatroom].usernames);
63+
});
64+
65+
// when the user disconnects.. perform this
66+
socket.on('disconnect', function() {
67+
// remove the username from global usernames list
68+
try
69+
{
70+
delete usernames[socket.username];
71+
delete chatrooms[socket.chatroom].usernames[username];
72+
chatrooms[socket.chatroom].usercount--;
73+
// echo globally that this client has left
74+
if (chatrooms[socket.chatroom].usercount > 0)
75+
{
76+
io.sockets.in(socket.chatroom).emit('updatechat', 'SERVER', socket.username + ' has disconnected');
77+
// update list of users in chat, client-side
78+
io.sockets.in(socket.chatroom).emit('updateusers', usernames);
79+
}
80+
81+
socket.leave(socket.chatroom);
82+
}
83+
catch(e)
84+
{
85+
console.log(socket.username, socket.chatroom, chatrooms);
86+
}
87+
88+
89+
});
90+
91+
});

0 commit comments

Comments
 (0)