Skip to content

Commit

Permalink
socket
Browse files Browse the repository at this point in the history
  • Loading branch information
git status committed Aug 11, 2017
1 parent 87418af commit b7b5803
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
9 changes: 5 additions & 4 deletions backend/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Document = models.Document;
///////////////////////////// END OF PUBLIC ROUTES /////////////////////////////

router.use(function(req, res, next){
console.log('REQ', req)

if (!req.user) {
res.redirect('/login');
} else {
Expand All @@ -31,16 +31,16 @@ router.use(function(req, res, next){
// });

router.post('/newdoc', function(req,res){
console.log("body", req.body);

// console.log("req.user?", req.user);
Document.findById(id, function(err, dock){
Document.findById(req.user._id, function(err, dock){
if (err){
console.log("error in doc find");
} else if (!dock){
var doc = new Document({
title: req.body.title,
date: req.body.date,
author: req.user,
author: req.user._id,
content: req.body.content
})
doc.save(function(err, doc) {
Expand Down Expand Up @@ -103,6 +103,7 @@ router.get('/protected', function(req, res, next) {
});



///////////////////////////// END OF PRIVATE ROUTES /////////////////////////////

module.exports = router;
31 changes: 29 additions & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var bodyParser = require('body-parser');
var passport = require('passport');
var LocalStrategy = require('passport-local');
var mongoose = require('mongoose');


//connecting to db
var connect = process.env.MONGODB_URI;
var REQUIRED_ENV = "SECRET MONGODB_URI".split(" ");
REQUIRED_ENV.forEach(function(el) {
Expand All @@ -24,6 +27,8 @@ var routes = require('./routes/routes');
var auth = require('./routes/auth');
//starting express
const app = express()
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(bodyParser.json())
// Example route
app.get('/', function (req, res) {
Expand Down Expand Up @@ -67,7 +72,7 @@ app.use('/', auth(passport));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');//unfound goes here
var err = new Error('Not Found');//unfound goes here
err.status = 404;
next(err);
});
Expand All @@ -94,6 +99,28 @@ app.use(function(req, res, next) {
// });
// });

app.listen(3000, function () {
io.on('connection', function (socket) {

socket.on('grabfile', function (data) {
console.log(data);
models.Document.findById({ _id: data.docid }, function (err, doc) {
// if there's an error, finish trying to authenticate (auth failed)
console.log('DOOOOOC', doc);
if (err) {
console.log('Error fetching doc', err);
socket.emit('errid', {err: err})
return done(err);
} else if(!doc){
console.log('not Found ')
} else {
console.log('HEEER', doc);
socket.emit('suc', {doc: doc.content})
}
});
});
})


server.listen(3000, function () {
console.log('Backend server for Electron App running on port 3000!')
})
2 changes: 2 additions & 0 deletions build/index.dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<div id="root"></div>
</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script src="http://localhost:8080/app.bundle.js"></script>

</html>

<!-- <!DOCTYPE html>
Expand Down
2 changes: 2 additions & 0 deletions build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
<div id="root"></div>
</body>


<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script src="./app.bundle.js"></script>
</html>
26 changes: 21 additions & 5 deletions reactApp/components/Docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Docs extends React.Component {
this.state={
modalOpen1: false,
modalOpen2: false,

docid: '',
title:'Untitled',
docs: []
}
Expand Down Expand Up @@ -56,6 +56,20 @@ class Docs extends React.Component {
this.props.history.push('/editor')

}
fetch(){
console.log(this.state.docid)
var socket = io.connect('http://localhost:3000');
socket.emit('grabfile', { docid: this.state.docid });
socket.on('errid', function(data){
alert('invalid: ', data)
})
socket.on('suc', (data)=>{
console.log('sucdoc returned', data)
console.log(this.props);
this.props.history.push('/editor')
})
}

render(){
var modalClass1 = classNames({
'modal': true,
Expand Down Expand Up @@ -93,9 +107,9 @@ class Docs extends React.Component {
<div className={modalClass2}>
<div className="modal-background" onClick={(e)=>this.modal2Toggle(e)}></div>
<div className="modal-content">
<div className="box">Doc Collab input
<input type='text'/>
<input type="submit" value="Create" onClick={(e)=>this.modal2Toggle(e)}/>
<div className="box">Enter doc id
<input type='text' onChange={(e)=>this.setState({docid: e.target.value})}/>
<input type="submit" value="Fetch" onClick={(e)=>{this.modal2Toggle(e), this.fetch()}}/>
</div>
</div>
<button className="modal-close is-large" onClick={(e)=>this.modal2Toggle(e)}></button>
Expand All @@ -107,6 +121,9 @@ class Docs extends React.Component {
{this.state.docs.map((doc)=><Link to={`/editor/${doc._id}`} key={doc._id} >{doc.title}</Link>)}

</div>
<div>
<button onClick={()=>this.props.history.push('/')}>Logout</button>
</div>
</div>
)
}
Expand All @@ -119,4 +136,3 @@ class Docs extends React.Component {
// );

export default Docs;

4 changes: 2 additions & 2 deletions reactApp/components/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ submit(){
email: this.state.email
})
.then((response)=> {
console.log("resp",response);
console.log("data",response.data);
// console.log("resp",response);
// console.log("data",response.data);
if(response.data==='welcome'){
self.props.history.push('/')
}
Expand Down

0 comments on commit b7b5803

Please sign in to comment.