2
嘗試我開始使用下面的錯誤DEBUG = *不能socket.io客戶端連接到服務器摩卡測試
mocha:runner run suite Chat Server Message Events +1ms
Message Events
socket.io-client:url parse http://localhost:5000 +1s
socket.io-client ignoring socket cache for http://localhost:5000 +0ms
socket.io-client:manager readyState closed +1ms
socket.io-client:manager opening http://localhost:5000 +2ms
engine.io-client:socket creating transport "websocket" +1ms
engine.io-client:socket setting transport websocket +7ms
socket.io-client:manager connect attempt will timeout after 20000 +1ms
socket.io-client:manager readyState opening +1ms
engine.io-client:socket socket error
{"description":
{"code":"ENOENT",
"errno":"ENOENT",
"syscall":"connect",
"target":{"domain":null,
"_events":{},
"_maxListeners":10,
"_socket":null,
"_ultron":null,
"_closeReceived":false,
"bytesReceived":0,
"readyState":0,
"supports":{"binary":true},
"extensions":{},
"_isServer":false,
"url":"ws://localhost:5000/socket.io/?EIO=3&transport=websocket",
"protocolVersion":13,
"binaryType":"buffer"}}} +2ms
這種情況不會發生socket.io,客戶端連接到節點服務器,當我在瀏覽器上使用socket.io-client,使用相同的套接字配置。
下面是測試代碼:
var server = require('myServer');
var io = require('socket.io-client');
var url = 'http://localhost:5000';
var options = {
transports: ['websocket'],
'force new connection': true
};
var sender;
describe("Server Test", function() {
it('Should login user', function (done) {
sender = io(url, options);
});
});
這裏是服務器代碼:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log("connect");
});
server.listen(5000);
服務器工作正常。它顯示scoket.io-client從瀏覽器運行但未進行測試時的日誌消息。
有人能給我一個線索爲什麼會發生這種情況?