在实践中,我有一个带有Node JS的Web应用程序,其结构如下:
socketProva.js + index.html
var io = require('socket.io')();
var fs = require('fs');
var data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
io.on('connection', function(socket) {
console.log('connected:', socket.client.id);
setInterval(function() {
data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
socket.emit('update-value', data);
console.log('message sent to the clients');
}, 1000);
});
io.listen(8080);
<!DOCTYPE html>
<html>
<head>
<!-- this changed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<h1>Mysql Real Time Data Trading on NodeJS</h1>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>Number</td>
</tr>
</thead>
<tbody id="incremental">
</tbody>
</table>
<script>
var socket = io('http://localhost:8080');
socket.on('update-value', function(data) {
console.log('received new value', data);
$('#incremental').append('<tr><td>' + data.id + '</td><td>' + data.number + '</td></tr>');
});
</script>
</body>
</html>
data.json
[ { "id": 1, "number": "12" }
,{ "id": 3, "number": "13.4" }
,{ "id": 4, "number": "13.4" }
,{ "id": 5, "number": "13.9" }
,{ "id": 6, "number": "13.9" }
,{ "id": 7, "number": "12.9" }
,{ "id": 8, "number": "12.9" }
,{ "id": 9, "number": "123" }
,{ "id": 10, "number": "123" }
,{ "id": 11, "number": "12345" }
,{ "id": 12, "number": "12345" }
,{ "id": 13, "number": "3" }
,{ "id": 14, "number": "3" }
,{ "id": 15, "number": "126" }
,{ "id": 16, "number": "126" }
,{ "id": 17, "number": "111" }
,{ "id": 18, "number": "123" }
,{ "id": 19, "number": "123" }
,{ "id": 20, "number": "1" }
,{ "id": 21, "number": "1" }
,{ "id": 22, "number": "13.4" }
,{ "id": 25, "number": "11" }
,{ "id": 26, "number": "11" }
,{ "id": 27, "number": "12342300" }
,{ "id": 30, "number": "6" }
,{ "id": 31, "number": "2" }
,{ "id": 33, "number": "2" }
,{ "id": 37, "number": "1" }
,{ "id": 39, "number": "1" }
,{ "id": 41, "number": "13" }
,{ "id": 42, "number": "17654" }
,{ "id": 43, "number": "2" }
]
存储过程-使用phpMyAdmin的MySQL
BEGIN
SELECT GROUP_CONCAT(CONCAT('{ "id": ' ,id, ', "number": "', number, '" } \r')) FROM NodeJS INTO @result;
SET @q = CONCAT('[ ', @result, ' ]');
SELECT @q INTO OUTFILE '/opt/brackets/www/node_modules/server/data.json';
END
因此,当前在我的Web应用程序中,服务器获取传递到客户端的json文件的数据,目前,存储过程是单独的存储过程,用于处理从mysql接收数据的json文件的生成具有两个字段(ID,数字)的表上的数据库,但当前尚未在Web应用程序机制内关联。 因此,请求如下:
非常感谢您的关注。