使用node.js和socket.io从url通过套接字推送数据

时间:2014-02-05 10:37:34

标签: node.js socket.io

我正在尝试使用node.js和socket.io从url通过套接字推送数据。我认为我的代码工作还远远不够。我收到一个错误,我不确定如何解决。

这是错误:

    /usr/local/bin/node_modules/socket.io/lib/manager.js:104
  server.on('error', function(err) {
         ^
TypeError: Object #<Object> has no method 'on'
    at new Manager (/usr/local/bin/node_modules/socket.io/lib/manager.js:104:10)
    at Object.exports.listen (/usr/local/bin/node_modules/socket.io/lib/socket.io.js:78:10)
    at Object.<anonymous> (/usr/local/bin/timeserver.js:6:30)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

知道我需要做些什么才能对此进行排序?任何帮助将不胜感激。

这是我的代码:

var http = require('http');
var str = '';

http.createServer(handler)
, io = require('socket.io').listen(http)
, fs = require('fs')

http.listen(8888);

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);
    });
}


    //The url we want is "myserver:8000/"
    var options = {
      host: 'myserver',
      path: '/getdata?param1=1&iparam2=1738517',
      //since we are listening on a custom port, we need to specify it by hand
      port: '8000',
      //This is what changes the request to a GET request
      method: 'GET'
    };

    callback = function(response) {

              response.on('data', function (chunk) {
                str += chunk;
              });

            response.on('end', function() {

            });
        }

        var req = http.request(options, callback);
        //This is the data we are posting, it needs to be a string or a buffer
        req.write("hello world!");
        req.end();





function sendData(socket){
    var thisRef = this;


    socket.emit('timeUpdate', { currentTime: str });

    //socket.emit('timeUpdate', { currentTime:  formattedTime});
    setTimeout(function(){
        sendData.call(thisRef,socket)
    },1000);
}



io.sockets.on('connection', function (socket) {
    socket.emit('welcomeMessage', { welcome: 'Welcome to server poller' });
    sendData(socket);
});

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。毫无疑问,这不是最优雅的代码,但它是一个开始:)

var http = require('http');
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')

app.listen(8888);


function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
    function (err, data) {
            if (err) {
                    res.writeHead(500);
                    return res.end('Error loading index.html');
            }else{
                    console.log('index.html loaded')
            }
            res.writeHead(200);
            res.end(data);
    });
}



function sendData(socket){
    var thisRef = this;

    //The url we want is "myserver:8000/"
    var options = 
    {
      host: 'myserver',
      path: '/getData?param1=1&param2=1738517',
      //since we are listening on a custom port, we need to specify it by hand
      port: '8000',
      //This is what changes the request to a GET request
      method: 'GET'
    };

    callback = function(response) 
    {
      var str = ''
      response.on('data', function (chunk) {
        str += chunk;
      });

      response.on('end', function () {
        console.log(str);
        socket.emit('timeUpdate', { currentTime: str });
      });


    }
        var req = http.request(options, callback);
        //This is the data we are posting, it needs to be a string or a buffer
        req.write("Hello World!");
        req.end();


    setTimeout(function()
    {


        sendData.call(thisRef,socket)
    },10000);
}



io.sockets.on('connection', function (socket) {


    socket.emit('welcomeMessage', { welcome: 'Welcome to server poller' });



    sendData(socket);
});