Browse Source

Support multiple Pads, fix bugs

master
Peter Cunha 4 years ago
parent
commit
c942728d5e
8 changed files with 54 additions and 23 deletions
  1. +1
    -1
      app.js
  2. +2
    -0
      bin/www
  3. +1
    -0
      package.json
  4. +24
    -8
      public/javascripts/worker.js
  5. +3
    -1
      routes/index.js
  6. +20
    -9
      routes/socket.js
  7. +2
    -2
      views/index.pug
  8. +1
    -2
      views/pad.pug

+ 1
- 1
app.js View File

@@ -26,7 +26,7 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/socket', socket);
app.use('/Pad', socket);

// catch 404 and forward to error handler
app.use(function(req, res, next) {

+ 2
- 0
bin/www View File

@@ -28,6 +28,7 @@ var server = http.createServer(app);
var io = app.io
io.attach(server);


/**
* Listen on provided port, on all network interfaces.
*/
@@ -35,6 +36,7 @@ io.attach(server);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
console.log("Started server on port", port)

/**
* Normalize a port into a number, string, or false.

+ 1
- 0
package.json View File

@@ -13,6 +13,7 @@
"morgan": "~1.7.0",
"pug": "~2.0.0-beta10",
"serve-favicon": "~2.3.2",
"shortid": "^2.2.6",
"socket.io": "^1.7.2"
},
"description": "Collaborative online notepad",

+ 24
- 8
public/javascripts/worker.js View File

@@ -1,16 +1,32 @@
var socket = io();
var cache = "";
var firstLoad = true;

var f = window.location.pathname;
var temp = f.split("/");
var pathname;

function processText() {
var x = document.getElementById("text").value;
if (cache = "") {
cache = x;
}
socket.emit('datagram', { my: x });
if (f.substr(f.length - 1) == "/") {
pathname = temp[temp.length - 2];
} else {
pathname = temp[temp.length - 1];
}

// Ask server latest data
socket.emit('sync', { path: pathname });

// Set TextArea as server response
socket.on('notify', function (data) {
console.log(data.content.my);
document.getElementById("text").value = data.content.my;
document.getElementById("text").value = data.content;
});

// Send latest data to server
function processText() {
var x = document.getElementById("text").value;

if (cache = "") {
cache = x;
}
socket.emit('datagram', { text: x, path: pathname });
}

+ 3
- 1
routes/index.js View File

@@ -1,9 +1,11 @@
var express = require('express');
var shortid = require('shortid')
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
var sid = shortid.generate();
res.render('index', { title: 'Pad', id: sid });
});

module.exports = router;

+ 20
- 9
routes/socket.js View File

@@ -1,23 +1,34 @@
module.exports = function(io) {
var app = require('express');
var router = app.Router();

var notepadContent = { my: '' };
var pages = new Map();

io.on('connection', function(socket) {
console.log("A user connected")
socket.emit('notify', { content: notepadContent });

socket.on('sync', function (data) {
if (pages.has(data.path)) {
socket.emit('notify', { content: pages.get(data.path) });
console.log("Paste accessed: " + data.path);
} else {
socket.emit('notify', { content: 'Pad created! Share the URL with a friend to edit text in real-time.' });
console.log("Created paste: " + data.path);
}
});

socket.on('datagram', function (data) {
console.log(data);
notepadContent = data;
socket.emit('notify', { content: notepadContent });
socket.broadcast.emit('notify', { content: notepadContent });
pages.set(data.path, data.text);
var curr = pages.get(data.path);

console.log(data.text);

socket.emit('notify', { content: curr });
socket.broadcast.emit('notify', { content: curr });
});
});

/* GET home page. */
router.get('/', function(req, res, next) {
/* GET pad by unique id */
router.get('/:id', function(req, res, next) {
res.render('pad', { title: 'Notepad' });
});


+ 2
- 2
views/index.pug View File

@@ -4,5 +4,5 @@ block content
h1= title
p Welcome to #{title}

script(src="https://cdn.socket.io/socket.io-1.4.5.js")
script var socket = io();
script.
document.location = "/Pad/#{id}/";

+ 1
- 2
views/pad.pug View File

@@ -8,5 +8,4 @@ html
script(src="/javascripts/worker.js", type="text/javascript")

body
textarea(id="text", onchange="processText()")
textarea(id="text", autocomplete="off", onchange="processText()")

Loading…
Cancel
Save