简体   繁体   English

Socket.IO Node.JS客户端显示变量。

[英]Socket.IO Node.JS Client Side Displaying Variable.

I am creating an application which in my eyes requires a variable that changes to be displayed in front of the client at any given time. 我正在创建一个应用程序,在我看来它需要一个变量,该变量在任何给定时间都将显示在客户端的前面。

A given example, user foo has $100 dollars. 给定的示例,用户foo有$ 100美元。 An event happens which deducts $20 dollars from user foo's account. 发生了一个事件,该事件从foo用户的帐户中扣除了20美元。 As soon as this deduction happens i would like the clients text to change to $80. 扣除后,我希望客户的短信更改为$ 80。

I have a fundamental understanding of how this needs to happen. 我对这是如何发生有基本的了解。

Here is the code I have, 这是我的代码,

app.js 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 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>

I can imagine it would have to be emitted by app.js, but i have not the idea on how to display it to the client. 我可以想象它必须由app.js发出,但是我不知道如何显示给客户端。 You can see in the index.html, i have where i would like the variable to be displayed. 您可以在index.html中看到,我在哪里想显示该变量。

You HTML is completely messed up, here is an example of working code, each time the client clicks on 'Buy', it will send a signal to the Node server, this one will receive it, discount $20 from his balance and send the updated balance to the client. 您的HTML完全混乱了,这是一个工作代码示例,每次客户端单击“购买”时,它将向节点服务器发送一个信号,该信号将得到它,从其余额中扣除20美元并发送更新的平衡给客户。

app.js: 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) {
    console.log('connected');
  socket.on('buy', function (data) {
    socket.emit('update balance', data-20); // Decrease 20 from the balance
  });

  socket.emit('news', { hello: 'world' });
});

index.html: index.html:

<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script>
                window.addEventListener("load", function(event) {
                    function updateBalance () {
                        document.getElementById("balance").innerText = balance;
                    }
                    var balance = 100;
                    updateBalance();
                    var socket = io(location.href.split( '/' )[2]); // Connect to same URL

                    socket.on('update balance', function (data) {
                        balance = data;
                        updateBalance();
                    });

                    document.getElementById("button").onclick = function () {
                        socket.emit('buy', balance);
                    };
                });
        </script>
    </head>
    <body>
        <p>Balance: $<span id="balance"></span></p><br/>
        <button id="button">Buy</button>
    </body>
</html>

I think socket in index.html should emit connection first. 我认为index.html套接字应该首先发出connection

try to change 尝试改变

var socket = io('http://192.168.1.50');

to

var socket = io('http://192.168.1.50');
socket.emit('connection', 'test');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM