socket.io?
, ...
(Note: "msg" object, for example, for future reference only)
index.html
<!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>Title</title>
</head>
<body>
<h1>Search for a movie</h1>
<p><a href="#" onclick="runPython()"> Run python </a></p>
<p><a id="table-data"> See the table </a></p>
<script src="/socket.io/socket.io.js"></script>
<script>
var msg = {
msg: "TestMsgSend",
iParam: 100,
sParam: "Test Param 1",
aParam: [1, 2, 3]
}
var socket = io();
socket.on("server:finished", function (msg) {
console.log("\nId: " + socket.id + "\nmsg: " + msg.msg + "\niParam: " + msg.iParam + "\nsParam: " + msg.sParam + "\naParam: " + msg.aParam);
document.getElementById("table-data").setAttribute("href", "/data");
});
function runPython() {
socket.emit("client:run", msg);
}
</script>
</body>
</html>
app.js
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
app.use("/", express.static(__dirname + "/"));
// ***************************************************************************
// ***************************************************************************
// ***** Your code
// ***************************************************************************
// ***************************************************************************
io.on("connection", function (socket) {
console.log("New connection with id: " + socket.id);
socket.on("client:run", function (msg) {
// msg = "TestMsgSend", for further usage
console.log("\nId: " + socket.id + "\nmsg: " + msg.msg + "\niParam: " + msg.iParam + "\nsParam: " + msg.sParam + "\naParam: " + msg.aParam);
// ***************************************************************************
// ***************************************************************************
// ***** Your code
// ***************************************************************************
// ***************************************************************************
msg.msg = "TestMsgReceived";
msg.iParam++;
msg.sParam = "Test Param 2";
msg.aParam.push(4, 5, 6)
io.emit("server:finished", msg);
});
});
// ***************************************************************************
// ***************************************************************************
// ***** Your code
// ***************************************************************************
// ***************************************************************************
http.listen(80, function () {
console.log("listening on *:80");
});
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.2",
"socket.io": "^2.0.4"
}
}
Start with node app.js and enjoy
source
share