TypeError:RFB不是函数

时间:2016-05-27 10:56:02

标签: javascript angularjs

我试图使用angularJS(see tutorial here)来强制使用vnc客户端,但在运行应用程序时遇到此错误: TypeError:RFB不是函数

这是server.js代码:

var RFB = require('rfb2'),
    io = require('socket.io'),
    Png = require('../node_modules/node-png/lib/png').Png,
    express = require('express'),
    http = require('http'),
    clients = [],
    Config = {
      HTTP_PORT: 8090
    };

function createRfbConnection(config, socket) {
  try {
    var r = RFB({
      host: 'config.hostname',
      port: config.port,
      password: config.password,
      securityType: 'vnc',
    });
  } catch (e) {
    console.log(e);
  }
  addEventHandlers(r, socket);
  return r;
}

function addEventHandlers(r, socket) {
  var initialized = false,
      screenWidth, screenHeight;

  function handleConnection(width, height) {
    screenWidth = width;
    screenHeight = height;
    console.info('RFB connection established');
    socket.emit('init', {
      width: width,
      height: height
    });
    clients.push({
      socket: socket,
      rfb: r,
      interval: setInterval(function () {
        r.requestRedraw();
      }, 1000)
    });
    r.requestRedraw();
    initialized = true;
  }

  r.on('error', function () {
    console.error('Error while talking with the remote RFB server');
  });

  r.on('raw', function (rect) {
    !initialized && handleConnection(rect.width, rect.height);
    socket.emit('frame', {
      x: rect.x,
      y: rect.y,
      width: rect.width,
      height: rect.height,
      image: encodeFrame(rect).toString('base64')
    });
    r.requestUpdate({
      x: 0,
      y: 0,
      subscribe: 1,
      width: screenWidth,
      height: screenHeight
    });
  });

  r.on('*', function () {
    console.error(arguments);
  });
}

function encodeFrame(rect) {
  var rgb = new Buffer(rect.width * rect.height * 3, 'binary'),
      offset = 0;

  for (var i = 0; i < rect.fb.length; i += 4) {
    rgb[offset++] = rect.fb[i + 2];
    rgb[offset++] = rect.fb[i + 1];
    rgb[offset++] = rect.fb[i];
  }
  var image = new Png(rgb, rect.width, rect.height, 'rgb');
  return image.encodeSync();
}

function disconnectClient(socket) {
  clients.forEach(function (client) {
    if (client.socket === socket) {
      client.rfb.end();
      clearInterval(client.interval);
    }
  });
  clients = clients.filter(function (client) {
    return client.socket === socket;
  });
}

exports.run = function () {
  var app = express(),
      server = http.createServer(app);

  app.use(express.static(__dirname + '/../../client/app'));
  server.listen(Config.HTTP_PORT);

  console.log('Listening on port', Config.HTTP_PORT);

  io = io.listen(server, { log: false });
  io.sockets.on('connection', function (socket) {
    console.info('Client connected');
    socket.on('init', function (config) {
      var r = createRfbConnection(config, socket);
      socket.on('mouse', function (evnt) {
        r.sendPointer(evnt.x, evnt.y, evnt.button);
      });
      socket.on('keyboard', function (evnt) {
        r.sendKey(evnt.keyCode, evnt.isDown);
        console.info('Keyboard input')
      });
      socket.on('disconnect', function () {
        disconnectClient(socket);
        console.info('Client disconnected')
      });
    });
  });
};

和错误:

Listening on port 8090
Client connected
[TypeError: RFB is not a function]
Missing error handler on `socket`.
TypeError: Cannot read property 'on' of undefined
    at addEventHandlers (C:\Users\Aqqalu\Desktop\test\VNC\proxy\lib\server.js:49:4)
    at createRfbConnection (C:\Users\Aqqalu\Desktop\test\VNC\proxy\lib\server.js:22:3)
    at Socket.<anonymous> (C:\Users\Aqqalu\Desktop\test\VNC\proxy\lib\server.js:114:15)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at Socket.onevent (C:\Users\Aqqalu\Desktop\test\VNC\proxy\node_modules\socket.io\lib\socket.js:335:8)
    at Socket.onpacket (C:\Users\Aqqalu\Desktop\test\VNC\proxy\node_modules\socket.io\lib\socket.js:295:12)
    at Client.ondecoded (C:\Users\Aqqalu\Desktop\test\VNC\proxy\node_modules\socket.io\lib\client.js:193:14)
    at Decoder.Emitter.emit (C:\Users\Aqqalu\Desktop\test\VNC\proxy\node_modules\component-emitter\index.js:134:20)
    at Decoder.add (C:\Users\Aqqalu\Desktop\test\VNC\proxy\node_modules\socket.io-parser\index.js:247:12)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

从我对文档的快速了解,在我看来,您忘记在createConnection对象上调用rfb方法。它似乎不是可调用的。

var rfb = require('rfb2');
var r = rfb.createConnection({
  host: '127.0.0.1',
  port: 5900,
  password: 'secret'
});