has no method ' 关闭'"-6ren"> has no method ' 关闭'"-我尝试使用 mySocketIoInstance.close() 关闭与 socket.io 实例关联的 http 服务器,但出现以下错误: Uncaught TypeError: Object #-6ren">
gpt4 book ai didi

javascript - Node.js Socket.IO "Uncaught TypeError: Object # has no method ' 关闭'"

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:17 25 4
gpt4 key购买 nike

我尝试使用 mySocketIoInstance.close() 关闭与 socket.io 实例关联的 http 服务器,但出现以下错误:

 Uncaught TypeError: Object #<Server> has no method 'close'
at Socket.<anonymous> (/home/jackson/projects/tcg/test/server/ServerLobbySpec.js:35:20)
at Socket.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
at Socket.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:128:10)
at Socket.onconnect (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:306:8)
at Socket.onpacket (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/socket.js:206:12)
at Manager.<anonymous> (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
at Manager.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-emitter/index.js:134:20)
at Manager.ondecoded (/home/jackson/projects/tcg/node_modules/socket.io-client/lib/manager.js:270:8)
at Decoder.<anonymous> (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/component-bind/index.js:21:15)
at Decoder.Emitter.emit (/home/jackson/projects/tcg/node_modules/socket.io-client/node_modules/socket.io-parser/node_modules/emitter/index.js:132:20)

这是我尝试关闭它的地方:

'use strict';

var http = require('http'),
expect = require('chai').expect,
socketIo = require('socket.io'),
socketIoClient = require('socket.io-client'),
ServerLobby = require('../../source/server/ServerLobby');

describe('ServerLobby', function () {

var port = 2468,
uri = 'http://localhost:' + port;

describe('is connectable', function () {

it('should connect', function (done) {

var httpServer = http.Server().listen(port),
io = socketIo(httpServer),
lobby = new ServerLobby(io),
socket = socketIoClient(uri + lobby.namespace);

socket.on('connect', function () {

// HERE
io.close();
// HERE

done();
});

});

});

});

我尝试注释掉lobbysocket代码,只是console.log'd io.close并得到未定义

我的版本是1.0.6。根据 socket.io/test/socket.io.js (也是 1.0.6),我应该能够通过以下方式关闭服务器:

var http = require('http').Server;
var io = require('..');
var expect = require('expect.js');

// ...

it('should be able to close sio sending a srv', function() {
var PORT = 54016;
var srv = http().listen(PORT);
var sio = io(srv);

var clientSocket = client(srv, {
reconnection: false
});

clientSocket.on('connect', function init() {
expect(sio.nsps['/'].sockets.length).to.equal(1);

// HERE
sio.close();
// HERE

});

});

我的设置看起来几乎一模一样。请指教。

最佳答案

examples在您包含的测试中,它显示了一个使用以不同方式初始化的服务器对象构造的 socket.io 对象。

试试这个:

var http = require('http').Server,
// ...
var httpServer = http().listen(port);

完整示例取自 link发布时:

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

app.listen(80);

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

io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});

关于javascript - Node.js Socket.IO "Uncaught TypeError: Object #<Server> has no method ' 关闭'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726437/

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