add socket.io 1.2.0

This commit is contained in:
progre 2014-11-03 17:11:03 +09:00
parent 5eba93f581
commit d68d470bbd
4 changed files with 224 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import io = require('socket.io');
import io = require('socket.io-0.9');
var socketManager = io.listen(80);

View File

@ -4,9 +4,9 @@
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='../node/node.d.ts' />
///<reference path='../../node/node.d.ts' />
declare module "socket.io" {
declare module "socket.io-0.9" {
import http = require('http');
export function listen(server: http.Server, options: any, fn: Function): SocketManager;

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

76
socket.io/socket.io.d.ts vendored Normal file
View File

@ -0,0 +1,76 @@
// Type definitions for socket.io 1.2.0
// Project: http://socket.io/
// Definitions by: PROGRE <https://github.com/progre/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='../node/node.d.ts' />
declare module 'socket.io' {
var server: SocketIOStatic;
export = server;
}
interface SocketIOStatic {
(): SocketIO.Server;
(srv: any, opts?: any): SocketIO.Server;
(port: number, opts?: any): SocketIO.Server;
(opts: any): SocketIO.Server;
listen: SocketIOStatic;
}
declare module SocketIO {
interface Server {
serveClient(v: boolean): Server;
path(v: string): Server;
adapter(v: any): Server;
origins(v: string): Server;
sockets: Namespace;
attach(srv: any, opts: any): Server;
attach(port: number, opts: any): Server;
listen(srv: any, opts: any): Server;
listen(port: number, opts: any): Server;
bind(srv: any): Server;
onconnection(socket: any): Server;
of(nsp: String): Namespace;
emit(name: string, ...args: any[]): Socket;
use(fn: Function): Namespace;
on(event: 'connection', listener: (socket: Socket) => void): any;
on(event: 'connect', listener: (socket: Socket) => void): any;
on(event: string, listener: Function): any;
}
interface Namespace extends NodeJS.EventEmitter {
name: String;
connected: { [id: number]: Socket };
use(fn: Function): Namespace
on(event: 'connection', listener: (socket: Socket) => void): any;
on(event: 'connect', listener: (socket: Socket) => void): any;
on(event: string, listener: Function): any;
}
interface Socket {
rooms: string[];
client: Client;
conn: Socket;
request: any;
id: string;
emit(name: string, ...args: any[]): Socket;
join(name: string, fn?: Function): Socket;
leave(name: string, fn?: Function): Socket;
to(room: string): Socket;
in(room: string): Socket;
on(event: string, listener: Function): any;
broadcast: Socket;
volatile: Socket;
}
interface Client {
conn: any;
request: any;
}
}