gpt4 book ai didi

javascript - 类型错误 : RFB is not a function

转载 作者:行者123 更新时间:2023-12-03 06:51:26 24 4
gpt4 key购买 nike

我尝试使用 angularJS ( see tutorial here ) 来实现 vnc 客户端,但在运行应用程序时出现此错误:TypeError: RFB is not a function

这是 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)

谁能帮我吗?

最佳答案

仅从文档中快速浏览一下,在我看来,您忘记了在 rfb 对象上调用 createConnection 方法。它似乎不能按原样调用。

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

关于javascript - 类型错误 : RFB is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37481669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com