jeudi 4 août 2016

Cannot connect socket.io-client to server in mocha testing

Trying to connect socket.io-client to node server I get the following error using DEBUG=*

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

This situation doesn't happen when I use socket.io-client on browser, using the same socket configuration.

Here is the testing code:

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

Here is server code:

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

Server is working correctly. It shows log message when scoket.io-client is running from browser but not in testing.

Could someone give me a clue why is this happening ?

Aucun commentaire:

Enregistrer un commentaire