服务器代码:
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服务器只看到套接字。我想问题应该在这里的某个地方。
您的80端口已被另一个应用程序使用。尝试将节点服务器切换到3000,然后连接到它。或者你可以关闭监听80的应用程序——通常是一个web服务器应用程序。
如果不清楚,我指的是以下内容:
server.listen(3000); // There's no need to specify the host when you're running it on localhost.
我能够得到以下代码工作为我的应用程序
我从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') });
});
});
});
});