Skip to content

Commit 16c17ff

Browse files
committed
Added chatrooms
1 parent 6d9c347 commit 16c17ff

9 files changed

+531
-33
lines changed

Controller/controller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ methods.login = function (email, password) {
1212
});
1313
} else {
1414
return Promise.reject({
15-
"username": result.name, "message": "failure"
15+
"username": result.name, "message": "Password incorrect"
1616
});
1717
}
1818
})
1919
.catch(error => {
2020
return Promise.reject({
21-
"message": "failure"
21+
"message": "Verify email/password"
2222
});
2323
})
2424

@@ -34,7 +34,7 @@ methods.signup = function (name, email, password, mobile) {
3434
.catch(error => {
3535
console.log(error);
3636
return Promise.reject({
37-
"message": "failure"
37+
"message": "Already signed up"
3838
});
3939
})
4040
}

chatroom.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const express = require('express')
2+
const app = express()
3+
var http = require('http').Server(app);
4+
var io = require('socket.io')(http);
5+
var chatrooms = [{ coursename: "Angular_4", numUsers: 0 }, { coursename: "PYTHON", numUsers: 0 }, { coursename: "NodeJS", numUsers: 0 }]
6+
7+
8+
for (let i of chatrooms) {
9+
var room = io.of('/' + i.coursename);
10+
room.on('connection', (socket) => {
11+
var addedUser = false;
12+
console.log("user connected");
13+
14+
// when the client emits 'new message', this listens and executes
15+
socket.on('new message', (data) => {
16+
console.log(data);
17+
// we tell the client to execute 'new message'
18+
socket.broadcast.emit('new message', {
19+
username: socket.username,
20+
message: data
21+
});
22+
});
23+
24+
// when the client emits 'add user', this listens and executes
25+
socket.on('add user', (username) => {
26+
if (addedUser) return;
27+
28+
// we store the username in the socket session for this client
29+
socket.username = username;
30+
++i.numUsers;
31+
addedUser = true;
32+
socket.emit('login', {
33+
numUsers: i.numUsers
34+
});
35+
// echo globally (all clients) that a person has connected
36+
socket.broadcast.emit('user joined', {
37+
username: socket.username,
38+
numUsers: i.numUsers
39+
});
40+
});
41+
42+
43+
// when the user disconnects.. perform this
44+
socket.on('disconnect', () => {
45+
if (addedUser) {
46+
--i.numUsers;
47+
48+
// echo globally that this client has left
49+
socket.broadcast.emit('user left', {
50+
username: socket.username,
51+
numUsers: i.numUsers
52+
});
53+
}
54+
});
55+
56+
});
57+
}
58+
http.listen(3000, '0.0.0.0', function () {
59+
console.log('Tutorial app listening on port 3000!')
60+
})

index.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Socket.IO chat</title>
5+
<style>
6+
* { margin: 0; padding: 0; box-sizing: border-box; }
7+
body { font: 13px Helvetica, Arial; }
8+
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
9+
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
10+
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
11+
#messages { list-style-type: none; margin: 0; padding: 0; }
12+
#messages li { padding: 5px 10px; }
13+
#messages li:nth-child(odd) { background: #eee; }
14+
</style>
15+
</head>
16+
<body>
17+
<ul id="messages"></ul>
18+
<form action="">
19+
<input id="m" autocomplete="off" /><button>Send</button>
20+
</form>
21+
<script src="/socket.io/socket.io.js"></script>
22+
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
23+
<script>
24+
$(function () {
25+
var socket = io();
26+
$('form').submit(function(){
27+
socket.emit('new message', $('#m').val());
28+
$('#m').val('');
29+
return false;
30+
});
31+
socket.on('new message', function(msg){
32+
$('#messages').append($('<li>').text(msg.message));
33+
});
34+
});
35+
</script>
36+
</body>
37+
</html>

index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const express = require('express')
2+
const app = express()
3+
var http = require('http').Server(app);
4+
var io = require('socket.io')(http);
5+
6+
app.get('/',function(req,res){
7+
res.sendFile(__dirname + '/index.html');
8+
})
9+
10+
io.on('connection', function(socket){
11+
console.log("user connected");
12+
socket.on('chat message', function(msg){
13+
console.log('message: ' + msg);
14+
});
15+
});
16+
http.listen(3000, '0.0.0.0', function () {
17+
console.log('Tutorial app listening on port 3000!')
18+
})

0 commit comments

Comments
 (0)