adding web sockets (part 1) #15
7 changed files with 78 additions and 10 deletions
|
@ -28,6 +28,7 @@
|
||||||
"body-parser": "^1.17.1",
|
"body-parser": "^1.17.1",
|
||||||
"connect-multiparty": "^2.0.0",
|
"connect-multiparty": "^2.0.0",
|
||||||
"express": "^4.15.2",
|
"express": "^4.15.2",
|
||||||
"nodemon": "^1.11.0"
|
"nodemon": "^1.11.0",
|
||||||
|
"socket.io": "^2.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
28
public/claim.html
Normal file
28
public/claim.html
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Spee.ch Claim</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>spee.ch</h1>
|
||||||
|
<p>spee.ch is a single-serving site that reads and publishes images to and from the <a href="https://lbry.io">LBRY</a> blockchain.</p>
|
||||||
|
<h3>Status:</h3>
|
||||||
|
<p id="status">your image is being retrieved</p>
|
||||||
|
<script src="/socket.io/socket.io.js"></script>
|
||||||
|
<script>
|
||||||
|
var socket = io();
|
||||||
|
console.log(document.URL)
|
||||||
|
var url = document.URL.substring(document.URL.indexOf('3000/') + 5); // get from the window?
|
||||||
|
// request the image through the socket
|
||||||
|
socket.emit("image-request", url);
|
||||||
|
|
||||||
|
// receive the image through the socket
|
||||||
|
socket.on("image-send", function(data){
|
||||||
|
console.log("data", data);
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
BIN
public/eagle.jpg
Normal file
BIN
public/eagle.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
|
@ -101,6 +101,9 @@
|
||||||
previewFile(); //calls the function named previewFile()
|
previewFile(); //calls the function named previewFile()
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
<script src="/socket.io/socket.io.js"></script>
|
||||||
|
<script>
|
||||||
|
var socket = io();
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -18,7 +18,7 @@ module.exports = function(app){
|
||||||
// receive the request
|
// receive the request
|
||||||
console.log(" >> POST request on /publish");
|
console.log(" >> POST request on /publish");
|
||||||
//console.log(">> req.files:", req.files)
|
//console.log(">> req.files:", req.files)
|
||||||
console.log(" >> req.body:", req.body)
|
//console.log(" >> req.body:", req.body)
|
||||||
|
|
||||||
// build the data needed to publish the file
|
// build the data needed to publish the file
|
||||||
var publishObject = {
|
var publishObject = {
|
||||||
|
@ -64,10 +64,9 @@ module.exports = function(app){
|
||||||
app.get("/:name", function(req, res){
|
app.get("/:name", function(req, res){
|
||||||
var name = req.params.name;
|
var name = req.params.name;
|
||||||
console.log(">> GET request on /" + name)
|
console.log(">> GET request on /" + name)
|
||||||
// publish a message to the cue
|
|
||||||
// queueApi.addNewTaskToQueue("return claim for " + req.params.name + " ...")
|
|
||||||
// retrieve the claim
|
// retrieve the claim
|
||||||
lbryApi.serveClaimBasedOnNameOnly(name, res);
|
//lbryApi.serveClaimBasedOnNameOnly(name, res);
|
||||||
|
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html'));
|
||||||
});
|
});
|
||||||
// route for the home page
|
// route for the home page
|
||||||
app.get("/", function(req, res){
|
app.get("/", function(req, res){
|
||||||
|
|
27
routes/sockets-routes.js
Normal file
27
routes/sockets-routes.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
// routes to export
|
||||||
|
module.exports = function(app) {
|
||||||
|
var http = require("http").Server(app);
|
||||||
|
var io = require("socket.io")(http);
|
||||||
|
|
||||||
|
io.on('connection', function(socket){
|
||||||
|
console.log('a user connected');
|
||||||
|
|
||||||
|
// trying to serve an image file from the server
|
||||||
|
socket.on('image-request', function(data){
|
||||||
|
// 1. retrieve the image from lbry via daemon
|
||||||
|
console.log("received image request for:", data)
|
||||||
|
// 2. emit updates as the image is being retrieved
|
||||||
|
|
||||||
|
// 3. serve the image back once it is retrieved
|
||||||
|
socket.emit("image-send", "test string for: " + data);
|
||||||
|
})
|
||||||
|
|
||||||
|
// handle disconnect
|
||||||
|
socket.on('disconnect', function(){
|
||||||
|
console.log('user disconnected');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return http;
|
||||||
|
}
|
18
server.js
18
server.js
|
@ -2,19 +2,29 @@
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var bodyParser = require('body-parser');
|
var bodyParser = require('body-parser');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
// set port
|
// set port
|
||||||
var PORT = 3000;
|
var PORT = 3000;
|
||||||
// initialize express
|
|
||||||
|
// initialize express app
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
// make express look in the public directory for assets (css/js/img)
|
// make express look in the public directory for assets (css/js/img)
|
||||||
app.use(express.static(__dirname + '/public'));
|
app.use(express.static(__dirname + '/public'));
|
||||||
// configure epress
|
|
||||||
|
// configure express app
|
||||||
app.use(bodyParser.json()); // for parsing application/json
|
app.use(bodyParser.json()); // for parsing application/json
|
||||||
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
||||||
// require in routes
|
|
||||||
|
// require express routes
|
||||||
require("./routes/api-routes.js")(app);
|
require("./routes/api-routes.js")(app);
|
||||||
require("./routes/html-routes.js")(app);
|
require("./routes/html-routes.js")(app);
|
||||||
|
|
||||||
|
// include socket.io functionality
|
||||||
|
// this wraps the server in sockets, to intercept incoming sockets requests
|
||||||
|
var http = require("./routes/sockets-routes.js")(app);
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
app.listen(PORT, function() {
|
http.listen(PORT, function() {
|
||||||
console.log("Listening on PORT " + PORT);
|
console.log("Listening on PORT " + PORT);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue