mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-01 11:50:54 +08:00
Add SignalR definitions and tests
This commit is contained in:
Vendored
+104
@@ -0,0 +1,104 @@
|
||||
// Type definitions for SignalR 1.0
|
||||
// Project: http://www.asp.net/signalr
|
||||
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
|
||||
/// <reference path="jquery-1.8.d.ts" />
|
||||
|
||||
interface HubMethod {
|
||||
(callback: (data: string) => void );
|
||||
}
|
||||
|
||||
interface SignalREvents {
|
||||
onStart: string;
|
||||
onStarting: string;
|
||||
onReceived: string;
|
||||
onError: string;
|
||||
onConnectionSlow: string;
|
||||
onReconnect: string;
|
||||
onStateChanged: string;
|
||||
onDisconnect: string;
|
||||
}
|
||||
|
||||
interface SignalR {
|
||||
events: SignalREvents;
|
||||
connectionState: any; // ???
|
||||
transports: any;
|
||||
|
||||
hub: HubConnection;
|
||||
id: string; // ???
|
||||
logging: bool;
|
||||
messageId: string; // ???
|
||||
url: string;
|
||||
|
||||
(url: string, queryString?: any, logging?: bool): SignalR;
|
||||
hubConnection(url?: string): SignalR;
|
||||
|
||||
log(msg: string, logging: bool): void;
|
||||
isCrossDomain(url: string): bool;
|
||||
changeState(connection: SignalR, expectedState: number, newState: number): bool;
|
||||
isDisconnecting(connection: SignalR): bool;
|
||||
|
||||
createHubProxy(hubName: string): SignalR;
|
||||
|
||||
start(): SignalR;
|
||||
start(callback: () => void ): SignalR;
|
||||
start(settings: ConnectionSettings): SignalR;
|
||||
start(settings: ConnectionSettings, callback: () => void ): SignalR;
|
||||
|
||||
|
||||
send(data: string): void;
|
||||
stop(async?: bool, notifyServer?: bool): void;
|
||||
|
||||
starting(handler: () => void ): SignalR;
|
||||
received(handler: (data: any) => void ): SignalR;
|
||||
error(handler: (error: any) => void ): SignalR;
|
||||
stateChanged(handler: (change: any) => void ): SignalR;
|
||||
disconnected(handler: () => void ): SignalR;
|
||||
connectionSlow(handler: () => void ): SignalR;
|
||||
sending(handler: () => void ): SignalR;
|
||||
reconnected(handler: () => void ): SignalR;
|
||||
|
||||
done(handler: () => void ): SignalR;
|
||||
fail(handler: () => void ): SignalR;
|
||||
}
|
||||
|
||||
interface HubProxy {
|
||||
(connection: HubConnection, hubName: string): HubProxy;
|
||||
init(connection: HubConnection, hubName: string): void;
|
||||
hasSubscriptions(): bool;
|
||||
on(eventName: string, callback: (msg) => void ): HubProxy;
|
||||
off(eventName: string, callback: (msg) => void ): HubProxy;
|
||||
invoke(methodName: string, ...args: any[]): JQueryDeferred;
|
||||
}
|
||||
|
||||
interface HubConnectionSettings {
|
||||
queryString?: string;
|
||||
logging?: bool;
|
||||
useDefaultPath?: bool;
|
||||
}
|
||||
|
||||
interface HubConnection extends SignalR {
|
||||
//(url?: string, queryString?: any, logging?: bool): HubConnection;
|
||||
proxies;
|
||||
received(callback: (data: { Id; Method; Hub; State; Args; }) => void ): void;
|
||||
createHubProxy(hubName: string): HubProxy;
|
||||
}
|
||||
|
||||
interface SignalRfn {
|
||||
init(url, qs, logging);
|
||||
}
|
||||
|
||||
interface ConnectionSettings {
|
||||
transport?;
|
||||
callback?;
|
||||
waitForPageLoad?: bool;
|
||||
jsonp?: bool;
|
||||
}
|
||||
|
||||
interface JQueryStatic {
|
||||
signalR: SignalR;
|
||||
connection: SignalR;
|
||||
hubConnection(url?: string, queryString?: any, logging?: bool): HubConnection;
|
||||
}
|
||||
@@ -44,6 +44,7 @@ Complete
|
||||
* [QUnit](http://qunitjs.com/) (by [Diullei Gomes](https://github.com/Diullei))
|
||||
* [Raphael](http://raphaeljs.com/) (by [CheCoxshall](https://github.com/CheCoxshall))
|
||||
* [Sammy.js](http://sammyjs.org/) (by [Boris Yankov](https://github.com/borisyankov))
|
||||
* [SignalR](http://www.asp.net/signalr) (by [Boris Yankov](https://github.com/borisyankov))
|
||||
* [SoundJS](http://www.createjs.com/#!/SoundJS) (by [Pedro Ferreira](https://bitbucket.org/drk4))
|
||||
* [Spin](http://fgnass.github.com/spin.js/) (by [Boris Yankov](https://github.com/borisyankov))
|
||||
* [Sugar](http://sugarjs.com/) (by [Josh Baldwin](https://github.com/jbaldwin/))
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/// <reference path="../Definitions/signalr-1.0.d.ts" />
|
||||
|
||||
function test_client() {
|
||||
var connection = $.connection('/echo');
|
||||
connection.received(function (data) {
|
||||
console.log(data);
|
||||
});
|
||||
connection.error(function (error) {
|
||||
console.warn(error);
|
||||
});
|
||||
connection.stateChanged(function (change) {
|
||||
if (change.newState === $.signalR.connectionState.reconnecting) {
|
||||
console.log('Re-connecting');
|
||||
}
|
||||
else if (change.newState === $.signalR.connectionState.connected) {
|
||||
console.log('The server is online');
|
||||
}
|
||||
});
|
||||
connection.reconnected(function () {
|
||||
console.log('Reconnected');
|
||||
});
|
||||
connection.start();
|
||||
connection.start(function () {
|
||||
console.log("connection started!");
|
||||
});
|
||||
connection.stop();
|
||||
connection.start().done(function () {
|
||||
console.log("connection started!");
|
||||
});
|
||||
connection.start({ transport: 'longPolling' });
|
||||
connection.start({ transport: $.signalR.transports.webSockets });
|
||||
connection.start({ transport: ['longPolling', 'webSockets'] });
|
||||
connection.start({ waitForPageLoad: false });
|
||||
connection.start({ transport: 'longPolling' }, function () {
|
||||
console.log('connection started!');
|
||||
});
|
||||
connection.send("Hello World");
|
||||
var connection = $.connection('http://localhost:8081/echo');
|
||||
connection.start({ jsonp: true });
|
||||
}
|
||||
|
||||
function test_connection() {
|
||||
var connection = $.connection('/echo');
|
||||
connection.received(function (data) {
|
||||
$('#messages').append('<li>' + data + '</li>');
|
||||
});
|
||||
connection.start();
|
||||
$("#broadcast").click(function () {
|
||||
connection.send($('#msg').val());
|
||||
});
|
||||
}
|
||||
|
||||
function test_hubs() {
|
||||
var chat = $.connection.chat;
|
||||
chat.client.addMessage = function (message) {
|
||||
$('#messages').append('<li>' + message + '</li>');
|
||||
};
|
||||
|
||||
$("#broadcast").click(function () {
|
||||
chat.server.send($('#msg').val());
|
||||
});
|
||||
$.connection.hub.start()
|
||||
.done(function () { alert("Now connected!"); })
|
||||
.fail(function () { alert("Could not Connect!"); });
|
||||
|
||||
$.connection.hub.logging = true;
|
||||
var myHub = $.connection.myHub;
|
||||
myHub.someState = "SomeValue";
|
||||
function connectionReady() {
|
||||
alert("Done calling first hub serverside-function");
|
||||
};
|
||||
myHub.SomeClientFunction = function () {
|
||||
alert("serverside called 'Clients.SomeClientFunction()'");
|
||||
};
|
||||
$.connection.hub.error(function () {
|
||||
alert("An error occured");
|
||||
});
|
||||
$.connection.hub.start()
|
||||
.done(function () {
|
||||
myHub.SomeFunction(SomeParam)
|
||||
.done(connectionReady);
|
||||
})
|
||||
.fail(function () {
|
||||
alert("Could not Connect!");
|
||||
});
|
||||
|
||||
$.connection.hub.url = 'http://localhost:8081/signalr'
|
||||
$.connection.hub.start();
|
||||
|
||||
var connection = $.hubConnection();
|
||||
var proxy = connection.createHubProxy('chat');
|
||||
var proxy = connection.createHubProxy('chat'),
|
||||
msg = 'hello',
|
||||
room = 'main';
|
||||
proxy.invoke('send', msg);
|
||||
proxy.invoke('send', msg, room);
|
||||
proxy.invoke('add', 1, 2)
|
||||
.done(function (result) {
|
||||
console.log('The result is ' + result);
|
||||
});
|
||||
proxy.on('addMessage', function (msg) {
|
||||
console.log(msg);
|
||||
});
|
||||
var connection = $.hubConnection('http://localhost:8081/');
|
||||
connection.start({ jsonp: true });
|
||||
}
|
||||
Reference in New Issue
Block a user