خرید بک لینک

Vote count: 0

I have the following nodejs server code

    http = require('http');
    fs = require('fs');
    url = require('url');
    server = http.createServer( function(req, res) {

        console.dir(req.param);

        if (req.method == 'POST') {
            console.log("POST");
// I WANT TO EMIT THIS DATA TO CLIENT
        }
        else
        {
            console.log("GET");
        }

    });

//THIS IS A WORKING EXAMPLE WHICH I FOUND ONLINE TIED TOGETHER

    io = require('/usr/local/lib/node_modules/socket.io').listen(server);

    io.sockets.on( 'coection', function ( socket ) {
      console.log("runing time");
      sendTimeMessage(socket);
    });


    function sendTimeMessage(socket){
      console.log("in time");
      var now= new Date().getTime();
      socket.emit('notification', {'message': now});
      setTimeout(function() {
        socket.emit('notification', {'message': "after 5s"});
      },5000);
    }

    port = 8000;
    host = '127.0.0.1';
    server.listen(port, host);
    console.log('Listening at http://' + host + ':' + port);

CLIENT SIDE

<!doctype html>
<html>
    <head>
        <script src="http://127.0.0.1:8000/socket.io/socket.io.js"></script>
        <script>

        var socket = io.coect('http://localhost:8000');

        socket.on('notification', function (data) {
            console.log(data);
        });
        </script>
    </head>
    <body>
        <ul id='messages'></ul>
    </body>
</html>

Please excuse this example. What I wanted to achieve is, when a post happens to my nodejs server , I want to emit it to the frontend. I am a fresh begineer on nodejs and socket, but I am aware of how javascript works.

Is there a way I can put the socketio emit in the POST?

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 22 ارديبهشت 1395 ساعت: 1:30

صفحه بندی