如何将server.js中从android客户端收到的数据显示为html文件?

时间:2018-02-07 10:45:27

标签: android node.js socket.io

我写了这个从Android客户端接收数据的node.js服务器。但是我试图在html页面中显示我无法显示的数据,我不知道如何在html页面中显示,因为我是初学者的NodeJS。我刚开始探索实时计算活跃用户数。

我的nodejs server.js代码

var net = require('net');
var sockets = [];
var connectCounter=0;
var svr = net.createServer()
svr.on("connection",function(sock) {
    connectCounter++;
  var remoteAddress = sock.remoteAddress + ":" + sock.remotePort;
  console.log("new client connection is made %s", remoteAddress);

    sockets.push(sock);

    sock.write('Welcome to the server!\n');
    sock.on('data', function(data) {
        console.log("Data from %s :%s", remoteAddress, data);
        sock.write("Hello", + data);
        console.log("connection_id:", + sock.id);
        console.log("Number of active state(connection) :",connectCounter);
      
      });
    sock.on('end', function() {
        connectCounter--;
        console.log('Disconnected: ' + sock.remoteAddress + ':' + sock.remotePort);
        console.log("Number of active state(disconnected) :",connectCounter);
        var idx = sockets.indexOf(sock);
        if (idx != -1) {
            delete sockets[idx];
        }
    });
});

var svraddr = 'localhost';
var svrport = 3000;

svr.listen(svrport, svraddr);
console.log('Server Created at ' + svraddr + ':' + svrport + '\n');

我的安卓代码

socket = new Client("localhost", 3000);
socket.setClientCallback(new Client.ClientCallback() {
           @Override
           public void onMessage(String message) {
               System.out.println("Socket_connection_msg" + message);
           }

           @Override
           public void onConnect(Socket sockett) {
               try {
               JSONObject jsonObject=new JSONObject();
               jsonObject.put("name" ,"MainActivity");
                   jsonObject.put("value" ,"45");
                   socket.send(jsonObject.toString());

               } catch (JSONException e) {
                   e.printStackTrace();
               }

               System.out.println("Socket_connection" + sockett.isConnected());
               //socket.disconnect();


           }

           @Override
           public void onDisconnect(Socket socket, String message) {
               System.out.println("Socket_connection_disconnect" + socket.isConnected()+ message);
           }

           @Override
           public void onConnectError(Socket socket, String message) {
               System.out.println("Socket_connection_error" + message);
           }
       });
       socket.connect();

我的控制台输出是

当新的Android设备连接时,活动状态会增加,如果用户退出应用程序,它将断开连接并且连接减少1。 my output

现在我需要在HTML页面中显示的响应

我是nodejs的新手,任何帮助都将不胜感激

提前谢谢。

0 个答案:

没有答案