| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- /**
- * Module dependencies.
- */
- var express = require('express')
- , stylus = require('stylus')
- , sio = require('socket.io')
- , path = require('path')
- , fs = require('fs');
- /**
- * App.
- */
- var app = express.createServer();
- /**
- * Initial port to listen to.
- */
- var port = 3000;
- /**
- * Transport to test with.
- */
- var args = process.argv.slice(2)
- , transport = args.length ? args[0] : 'xhr-polling';
- /**
- * A map of tests to socket.io ports we're listening on.
- */
- var testsPorts = {};
- /**
- * App configuration.
- */
- app.configure(function () {
- app.use(stylus.middleware({ src: __dirname + '/public' }))
- app.use(express.static(__dirname + '/public'));
- app.use('/test', express.static(__dirname + '/../../test'));
- app.set('views', __dirname);
- app.set('view engine', 'jade');
- });
- /**
- * App routes.
- */
- app.get('/', function (req, res) {
- res.render('index', {
- layout: false
- , testsPorts: testsPorts
- , transport: transport
- });
- });
- /**
- * App listen.
- */
- app.listen(port++, function () {
- var addr = app.address();
- console.error(' listening on http://' + addr.address + ':' + addr.port);
- });
- /**
- * Override handler to simplify development
- */
- function handler (req, res) {
- fs.readFile(__dirname + '/../../dist/socket.io.js', 'utf8', function (err, b) {
- if (err) {
- res.writeHead(404);
- res.end('Error');
- return;
- }
- res.writeHead(200, { 'Content-Type': 'application/javascript' });
- res.end(b);
- });
- };
- /**
- * Socket.IO default server (to serve client)
- */
- var io = sio.listen(app);
- io.configure(function () {
- io.set('browser client handler', handler);
- io.set('transports', [
- transport
- ]);
- });
- /**
- * Scopes servers for a given test suite.
- */
- var currentSuite;
- function suite (name, fn) {
- currentSuite = testsPorts[name] = {};
- fn();
- };
- /**
- * Creates a socket io server
- */
- function server (name, fn) {
- currentSuite[name] = port;
- var io = sio.listen(port);
- io.configure(function () {
- io.set('transports', [transport]);
- });
- fn(io);
- port++;
- };
- /**
- * Socket.IO servers.
- */
- suite('socket.test.js', function () {
- server('test connecting the socket and disconnecting', function (io) {
- io.sockets.on('connection', function () {});
- });
- server('test receiving messages', function (io) {
- io.sockets.on('connection', function (socket) {
- var messages = 0;
- var interval = setInterval(function () {
- socket.send(++messages);
- if (messages == 3) {
- clearInterval(interval);
- setTimeout(function () {
- socket.disconnect();
- }, 500);
- }
- }, 50);
- });
- });
- server('test sending messages', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- socket.send(msg);
- });
- });
- });
- server('test manual buffer flushing', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- socket.send(msg);
- });
- });
- });
- server('test automatic buffer flushing', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- socket.send(msg);
- });
- });
- });
- server('test acks sent from client', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.send('tobi', function () {
- socket.send('tobi 2');
- });
- });
- });
- server('test acks sent from server', function (io) {
- io.sockets.on('connection', function (socket) {});
- });
- server('test connecting to namespaces', function (io) {
- io.of('/woot').on('connection', function (socket) {
- socket.send('connected to woot');
- });
- io.of('/chat').on('connection', function (socket) {
- socket.send('connected to chat');
- });
- });
- server('test disconnecting from namespaces', function (io) {
- io.of('/a').on('connection', function (socket) {});
- io.of('/b').on('connection', function (socket) {});
- });
- server('test authorizing for namespaces', function (io) {
- io.of('/a')
- .authorization(function (data, fn) {
- fn(null, false);
- })
- .on('connection', function (socket) {});
- });
- server('test sending json from server', function (io) {
- io.sockets.on('connection', function (socket) {
- io.sockets.json.send(3141592);
- });
- });
- server('test sending json from client', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (arr) {
- if (Array.isArray(arr) && arr.length == 3) {
- socket.send('echo');
- }
- });
- });
- });
- server('test emitting an event from server', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.emit('woot');
- });
- });
- server('test emitting multiple events at once to the server', function (io) {
- io.sockets.on('connection', function (socket) {
- var messages = [];
- socket.on('print', function (msg) {
- if (messages.indexOf(msg) >= 0) {
- console.error('duplicate message');
- }
- messages.push(msg);
- if (messages.length == 2) {
- socket.emit('done');
- }
- });
- });
- });
- server('test emitting an event to server', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('woot', function () {
- socket.emit('echo');
- });
- });
- });
- server('test emitting an event from server and sending back data', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.emit('woot', 1, function (a) {
- if (a === 'test') {
- socket.emit('done');
- }
- });
- });
- });
- server('test emitting an event to server and sending back data', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('tobi', function (a, b, fn) {
- if (a === 1 && b === 2) {
- fn({ hello: 'world' });
- }
- });
- });
- });
- server('test encoding a payload', function (io) {
- io.of('/woot').on('connection', function (socket) {
- var count = 0;
- socket.on('message', function (a) {
- if (a == 'ñ') {
- if (++count == 4) {
- socket.emit('done');
- }
- }
- });
- });
- });
- server('test sending query strings to the server', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.json.send(socket.handshake);
- })
- });
- server('test sending newline', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- if (msg == '\n') {
- socket.emit('done');
- }
- });
- });
- });
- server('test sending unicode', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- if (msg.test == "☃") {
- socket.emit('done');
- }
- });
- });
- });
- server('test webworker connection', function (io) {
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (msg) {
- if (msg == 'woot') {
- socket.emit('done');
- }
- });
- });
- });
- });
|