如何从单独的文件中获取输入并将输出发送到node.js中的另一个文件?

时间:2016-07-25 11:48:56

标签: node.js websocket socket.io

我有两个文件。一个是output.html,另一个是input.html。我正在从output.html获取输入,我想将输出数据打印到 <!DOCTYPE html> <html> <head> <style type="text/css"> #chatlog{ list-style:none;} #chatlog li{ border-bottom:2px solid #ddd; padding:10px 0} </style> <script src="/socket.io/socket.io.js"></script> <script type="text/javascript"> var socketio = io.connect("127.0.0.1:1337"); function sendMessage() { var msg = document.getElementById("message_input").value; socketio.emit("message_to_server", { message : msg}); } </script> </head> <body> <input type="text" id="message_input"/> <button onclick="sendMessage()">send</button> </body> </html> 。我该怎么做?

请帮助我。

input.html

    <!DOCTYPE html>
    <html>
        <head>
        <style type="text/css">

        #chatlog{ list-style:none;}
        #chatlog li{ border-bottom:2px solid #ddd; padding:10px 0}
         </style>
     <script src="/socket.io/socket.io.js"></script>
              <script src="../main.js"></script>
            <script type="text/javascript">

       var socketio = io.connect("127.0.0.1:1337");
    socketio.on("message_to_client", function(data) {
                    alert(gbldata);
        /*document.getElementById("chatlog").innerHTML = ("<hr/>" +
        data['message'] + document.getElementById("chatlog").innerHTML);*/
        var rediv =document.createElement('li');
        var retext= document.createTextNode(data['message']+"coming new call");
        rediv.appendChild(retext);
        var mul = document.getElementById('chatlog');
        //mul.appendChild(rediv);
        mul.insertBefore(rediv, mul.childNodes[0]);

        })
           </script>

        </head>
        <body>

            <ul id="chatlog">

            </ul>
        </body>
    </html>

output.html

    var http = require('http'),
        fs = require('fs');

    var app = http.createServer(function (request, response) {
        fs.readFile("client.html", 'utf-8', function (error, data) {
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(data);
            response.redirect('output.html');

      });
    }).listen(1337);
    console.log('Server running at http://localhost:1337/');

    var io = require('socket.io').listen(app);

    io.sockets.on('connection', function(socket) {
        socket.on('message_to_server', function(data) {
            io.sockets.emit("message_to_client",{ message: data["message"] });
            console.log(data["message"]);

        });
    });

server.js

onLocationChanged

0 个答案:

没有答案