我的服务器代码:
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
function init() {
server.listen(80, "0.0.0.0");
app.get("/", function (req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.use(express.static("public"));
}
测试文件:
var expect = require('chai').expect;
var io = require('socket.io-client');
var SocketTester = require('socket-tester');
var app = require('../server');
var socketUrl = 'http://127.0.0.1:3000';
var options = {
transports: ['websocket'],
'force new connection': true
};
var socketTester = new SocketTester(io, socketUrl, options);
describe('Sockets', function () {
var client1, client2, client3;
it('should send and receive a message', function (done) {
client1 = io.connect(socketUrl, options);
});
});
测试文件给出了一个错误:
Error: listen EADDRINUSE 0.0.0.0:80
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1231:14)
at listen (net.js:1267:10)
at net.js:1376:9
at node.js:933:3
如何使用测试文件与服务器联系?
我不确定这里会发生什么:
var server = require("http").Server(app);
var io = require("socket.io")(server);
但是,如果我没有错误,那么HTTP服务器只会看到socket.io,我想问题应该在这里的某处。
答案 0 :(得分:0)
您的端口80已被其他应用程序使用。尝试将节点服务器切换到3000,然后连接到它。或者,您可以关闭80上的应用程序 - 通常是Web服务器应用程序。
如果不清楚,我指的是以下内容:
server.listen(3000); // There's no need to specify the host when you're running it on localhost.
答案 1 :(得分:0)
我能够为我的应用程序提供以下代码
我从chai-http
借用了端口配置我相信这段代码会找到一个未使用的端口
//During the test the env variable is set to test
process.env.NODE_ENV = 'test';
var chai = require('chai');
var expect = chai.expect
var http = require('http');
var https = require('https');
var User = require.main.require('models/User');
// https://gist.github.com/branneman/8048520#7-the-wrapper
var app = require.main.require('app');
/**
* Create HTTP server.
*/
var server = http.createServer(app);
// From chai-http
function serverAddress (server, path) {
if ('string' === typeof server) {
return server + path;
}
var addr = server.address();
if (!addr) {
server.listen(0);
addr = server.address();
}
var protocol = (server instanceof https.Server) ? 'https' : 'http';
// If address is "unroutable" IPv4/6 address, then set to localhost
if (addr.address === '0.0.0.0' || addr.address === '::') {
addr.address = '127.0.0.1';
}
return protocol + '://' + addr.address + ':' + addr.port + path;
}
var socketURL = serverAddress(server,'/')
var options ={
transports: ['websocket'],
'force new connection': true
};
/**
* Enable Socket.IO
*/
var io = require('socket.io')(server);
require('../../sockets')(io);
var ioClient = require('socket.io-client');
describe('Players Sockets', (done) => {
var bonzo, magoo, fido;
beforeEach((done) => { //Before each test we reset the database
User.query().del().then(() => {
var bonzoPromise = new User({name: 'bonzo', authtype: 'github', authid: '12345678'}).save();
var magooPromise = new User({name: 'magoo', authtype: 'github', authid: '88345678'}).save();
var fidoPromise = new User({name: 'fido', authtype: 'github', authid: '93231678'}).save();
Promise.all([bonzoPromise,magooPromise,fidoPromise])
.then((users)=>{
bonzo = users[0];
magoo = users[1];
fido = users[2];
done();
})
})
});
it('Should broadcast updated players when user disconnects', function(done){
var clientFido = ioClient.connect(socketURL, options);
clientFido.on('connect', function(data){
clientFido.emit('whoami', { id: fido.id, name: fido.get('name') });
var clientBonzo = ioClient.connect(socketURL, options);
clientBonzo.on('connect', function(data){
clientBonzo.emit('whoami', { id: bonzo.id, name: bonzo.get('name') });
var clientMagoo = ioClient.connect(socketURL, options);
clientMagoo.on('connect', function(data){
clientMagoo.emit('whoami', { id: magoo.id, name: magoo.get('name') });
clientMagoo.on('players', function(message){
if(message.players.length < 3){ return; } // Ignore our own connect messages
expect(message.players).to.be.a('array');
expect(message.players.length).to.eq(3);
expect(message.players.map((player)=>{ return player.name})).to.include.members([bonzo.get('name'), magoo.get('name'), fido.get('name')]);
clientBonzo.on('players', function(message){
expect(message.players).to.be.a('array');
expect(message.players.length).to.eq(2);
expect(message.players.map((player)=>{ return player.name})).to.include.members([bonzo.get('name'), fido.get('name')]);
clientBonzo.disconnect();
clientFido.disconnect();
done();
});
clientMagoo.disconnect();
});
});
});
});
});
it('Should broadcast updated players when user connects', function(done){
var clientFido = ioClient.connect(socketURL, options);
clientFido.on('connect', function(data){
clientFido.emit('whoami', { id: fido.id, name: fido.get('name') });
clientFido.on('players', function(message){
expect(message.players).to.be.a('array');
if(message.players.length == 1){ return; } // Ignore our own connect message
expect(message.players.length).to.eq(2);
expect(message.players.map((player)=>{ return player.name})).to.include.members([bonzo.get('name'), fido.get('name')]);
clientBonzo.disconnect();
clientFido.disconnect();
done();
});
var clientBonzo = ioClient.connect(socketURL, options);
clientBonzo.on('connect', function(data){
clientBonzo.emit('whoami', { id: bonzo.id, name: bonzo.get('name') });
});
});
});
});