Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Full Node With Mongo Connection
  • Loading branch information
shrihariharanba committed Sep 2, 2018
1 parent eb2795f commit 1f48645
Show file tree
Hide file tree
Showing 27,084 changed files with 3,407,546 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .gitignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules/
24 changes: 24 additions & 0 deletions Day-10-Chat-Room/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const app = express();
const http = require('http');
const server = http.Server(app);
const port = 7002;
const io = require('socket.io')(server);
const chatLib = require('./lib/chat.lib');
const newsLib = require('./lib/news.lib');


app.get('/chat', function(req, res){
res.sendFile(__dirname + '/public/chat.html');
});

app.get('/news', function(req, res){
res.sendFile(__dirname + '/public/news.html');
});

chatLib.socketFunc(io);
newsLib.socketFunc(io);

server.listen(port, () => {
console.log(`Server started on port ${port}`)
});
31 changes: 31 additions & 0 deletions Day-10-Chat-Room/lib/chat.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const socketFunc = (io) => {
const chat = io.of('/chat');
let onlineUsers = [];
chat.on('connection', function (socket){
console.log('a user connected');
socket.on('user', function(user) {
console.log('Connected user', user);
onlineUsers.push(user);
socket.user = user;
socket.broadcast.emit('chat message', socket.user+" came online");
});
socket.on('chat message', function(msg) {
chat.emit('chat message', msg);
});

socket.on('disconnect', function() {
console.log('User Disconnected');
if (socket.user) {
let index = onlineUsers.indexOf(socket.user);
onlineUsers.splice(index, 1);
socket.broadcast.emit('chat message', socket.user+" left the chat");
}
});
});

}

module.exports = {
socketFunc
}

21 changes: 21 additions & 0 deletions Day-10-Chat-Room/lib/news.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const socketFunc = (io) => {
const news = io.of('/news');

news.on('connection', function (socket){
console.log('gajga a user connected');

socket.on('chat message', function(msg) {
news.emit('chat message', msg);
});

socket.on('disconnect', function() {
console.log('User Disconnected');
});
});

}

module.exports = {
socketFunc
}

39 changes: 39 additions & 0 deletions Day-10-Chat-Room/news.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
$(function () {
var socket = io('/chat'); // io('http://localhost', {path: '/nodejs/socket.io'});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});

socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>
Loading

0 comments on commit 1f48645

Please sign in to comment.