`socket`&上缺少错误处理程序TypeError:undefined不是节点js

时间:2016-09-27 07:59:29

标签: javascript node.js express socket.io

我在这里有一点问题,我想用nodejs制作简单的聊天应用程序,但在终端这个错误出来了,当然在网页浏览器聊天应用程序不起作用,这是我的代码

    // # SimpleServer
    //
    // A simple chat server using Socket.IO, Express, and Async.
    //
    var http = require('http');
    var path = require('path');

    var async = require('async');
    var socketio = require('socket.io');
    var express = require('express');



    //set this to your project URL
    var c9server = 'http://sne.dlinkddns.com:8080/politan/writeData.php';

    //ssl issues in c9
    process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';

    //
    // ## SimpleServer `SimpleServer(obj)`
    //
    // Creates a new instance of SimpleServer with the following options:
    //  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
    //
    var router = express();
    var server = http.createServer(router);
    var io = socketio.listen(server);

    router.use(express.static(path.resolve(__dirname, 'client')));
    var messages = [];
    var sockets = [];

    // create connection to file
    // var fs = require('fs');
    // var writeStream = fs.createWriteStream('log.json');


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


        messages.forEach(function (data) {
          socket.emit('message', data);
        });

        sockets.push(socket);

        socket.on('disconnect', function () {
          sockets.splice(sockets.indexOf(socket), 1);
          updateRoster();
        });

        socket.on('message', function (msg) {
          var text = String(msg || '');

          if (!text)
            return;

          var d = new Date();

          socket.get('name', function (err, name) {
            var data = {
              name: name,
              text: text,
              date: d
            };

            var trendData = {
              date: d,
              count: '1'
            };

            broadcast('message', data);
            messages.push(data);

            //log data
            logData(data, 'messages.json');
            logData(trendData, 'trend.json');

            //write data to mysql
            postData('data='+JSON.stringify(data), c9server);


          });
        });

        socket.on('identify', function (name) {
          socket.set('name', String(name || 'Anonymous'), function (err) {
            updateRoster();
          });
        });
      });

    function updateRoster() {
      async.map(
        sockets,
        function (socket, callback) {
          socket.get('name', callback);
        },
        function (err, names) {
          broadcast('roster', names);
        }
      );
    }

    function broadcast(event, data) {
      sockets.forEach(function (socket) {
        socket.emit(event, data);
      });
    }

    function logData(data, file){

      //setup file
      var fs = require('fs');

      //append the new data to the log
      fs.appendFile(file, JSON.stringify(data)+"\n", function (err) {
        if (err) throw err;
        console.log(JSON.stringify(data) +' was appended to file!');
      });

    }

    function postData(data, postUrl){

      var request = require('request');

      // Set the headers
      var headers = {
          'User-Agent':       'Super Agent/0.0.1',
          'Content-Type':     'application/x-www-form-urlencoded'
      }

      // Configure the request
      var options = {
          url: postUrl,
          method: 'POST',
          headers: headers,
          form: data
      }

      // Start the request
      request(options, function (error, response, body) {
          if (!error && response.statusCode == 200) {
              // Print out the response body
              console.log(body)
          } else {
              console.log(error);
          }

      })

    }

    // server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
    server.listen(8081, process.env.IP, function(){
      var addr = server.address();
      console.log("Chat server listening at", addr.address + ":" + addr.port);
    });

控制台中的错误

    Missing error handler on `socket`.
    TypeError: undefined is not a function
        at Socket.<anonymous> (/var/www/html/politan/server.js:89:14)
        at Socket.emit (events.js:107:17)
        at Socket.onevent (/var/www/html/politan/node_modules/socket.io/lib/socket.js:335:8)
        at Socket.onpacket (/var/www/html/politan/node_modules/socket.io/lib/socket.js:295:12)
        at Client.ondecoded (/var/www/html/politan/node_modules/socket.io/lib/client.js:193:14)
        at Decoder.Emitter.emit (/var/www/html/politan/node_modules/component-emitter/index.js:134:20)
        at Decoder.add (/var/www/html/politan/node_modules/socket.io-parser/index.js:247:12)
        at Client.ondata (/var/www/html/politan/node_modules/socket.io/lib/client.js:175:18)
        at Socket.emit (events.js:107:17)
        at Socket.onPacket (/var/www/html/politan/node_modules/engine.io/lib/socket.js:101:14)

很高兴你能帮助我的小项目,谢谢

0 个答案:

没有答案