Added the test files for the legacy socket.io code

This commit is contained in:
Damian Connolly 2015-07-11 21:33:48 +02:00
parent 812ed1d1a8
commit 9c47c6a65a
2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/// <reference path="socket.io-client.d.ts"/>
function testUsingWithNodeHTTPServer() {
var socket = io('http://localhost');
socket.on('news', function (data: any) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
}
function testUsingWithExpress() {
var socket = io.connect('http://localhost');
socket.on('news', function (data: any) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
}
function testUsingWithTheExpressFramework() {
var socket = io.connect('http://localhost');
socket.on('news', function (data: any) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
}
function testRestrictingYourselfToANamespace() {
var chat = io.connect('http://localhost/chat')
, news = io.connect('http://localhost/news');
chat.on('connect', function () {
chat.emit('hi!');
});
news.on('news', function () {
news.emit('woot');
});
}
function testSendingAndGettingData() {
var socket = io();
socket.on('connect', function () {
socket.emit('ferret', 'tobi', function (data: any) {
console.log(data);
});
});
}
function testUsingItJustAsACrossBrowserWebSocket() {
var socket = io('http://localhost/');
socket.on('connect', function () {
socket.emit('hi');
socket.on('message', function (msg: any) {
});
});
}

View File

@ -0,0 +1,145 @@
import socketIO = require('socket.io');
function testUsingWithNodeHTTPServer() {
var app = require('http').createServer(handler);
var io = socketIO(app);
var fs = require('fs');
app.listen(80);
function handler(req: any, res: any) {
fs.readFile(__dirname + '/index.html',
function (err: any, data: any) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data: any) {
console.log(data);
});
});
}
function testUsingWithExpress() {
var app = require('express')();
var server = require('http').Server(app);
var io = socketIO(server);
server.listen(80);
app.get('/', function (req: any, res: any) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data: any) {
console.log(data);
});
});
}
function testUsingWithTheExpressFramework() {
var app = require('express').createServer();
var io = socketIO(app);
app.listen(80);
app.get('/', function (req: any, res: any) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data: any) {
console.log(data);
});
});
}
function testSendingAndReceivingEvents() {
var io = socketIO(80);
io.on('connection', function (socket) {
io.emit('this', { will: 'be received by everyone' });
socket.on('private message', function (from: any, msg: any) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
io.sockets.emit('user disconnected');
});
});
}
function testRestrictingYourselfToANamespace() {
var io = socketIO.listen(80);
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
});
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
});
});
var news = io
.of('/news')
.on('connection', function (socket) {
socket.emit('item', { news: 'item' });
});
}
function testSendingVolatileMessages() {
var io = socketIO.listen(80);
io.sockets.on('connection', function (socket) {
var tweets = setInterval(function () {
socket.volatile.emit('bieber tweet', {});
}, 100);
socket.on('disconnect', function () {
clearInterval(tweets);
});
});
}
function testSendingAndGettingData() {
var io = socketIO.listen(80);
io.sockets.on('connection', function (socket) {
socket.on('ferret', function (name: any, fn: any) {
fn('woot');
});
});
}
function testBroadcastingMessages() {
var io = socketIO.listen(80);
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('user connected');
});
}
function testUsingItJustAsACrossBrowserWebSocket() {
var io = socketIO.listen(80);
io.sockets.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
}