我正在创建一个应用程序,这个应用程序在我眼中需要一个在任何给定时间都会在客户端前显示的变量。Socket.IO Node.JS客户端显示变量。
举个例子,用户foo有$ 100美元。 发生的事件会从用户foo的帐户中扣除20美元。 一旦这种扣除发生,我希望客户的文字更改为80美元。
我对这需要如何发生有基本的了解。
下面是代码我有,
app.js
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
var balance = '100';
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
的index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://192.168.1.50');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
<html><p>Balance: balancehere.</p> </html>
</script>
我能想象它必须由app.js发射,但我不知道如何将其展示给客户。你可以在index.html中看到,我有我想要显示的变量。