From 4a61f4eccd1f334ac07c24f68e8668b7e7913f37 Mon Sep 17 00:00:00 2001 From: Nax Date: Thu, 10 Dec 2015 20:18:16 +0100 Subject: [PATCH 1/2] Added socketty v0.2.2 --- socketty/socketty.d.ts | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 socketty/socketty.d.ts diff --git a/socketty/socketty.d.ts b/socketty/socketty.d.ts new file mode 100644 index 000000000..9cbbeeef9 --- /dev/null +++ b/socketty/socketty.d.ts @@ -0,0 +1,57 @@ +// Type definitions for Socketty v0.2.2 +// Project: https://www.npmjs.com/package/socketty +// Definitions by: Nax +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare var socketty: Socketty; + +declare module 'socketty' { + export = socketty; +} + +interface Socketty { + /** + * Connect to a socketty server. + * @param url The server url + * @param callback The callback to be run when the connection is open + * @return A Socket + */ + connect(url: string, callback: (SockettySocket) => void): SockettySocket; + + /** + * Create a socketty server. + * @param httpServer The HTTP server to use + * @return A socketty server + */ + createServer(httpServer: any): void; +} + +interface SockettySocket { + /** + * Listen for an action. + * @param action The action to listen to + * @param callback A callback to be run when the action is fired + */ + on(action: string, callback: (any?) => void): void; + + /** + * Send an action, as well as an optional message. + * @param action The action to send + * @param message The message to send + */ + send(action: string, message?: any): void; + + /** + * Specify a callback to be run when the socket is disconnected. + * @param callback The disconnect callback + */ + disconnect(callback: () => void): void; +} + +interface SockettyServer { + /** + * Specify a callback to be run when a new socket connects to the server. + * @param callback The callback + */ + connection(callback: (SockettySocket) => void): void; +} From 6f04aca222d233a4c610b5020a09c760140d33f5 Mon Sep 17 00:00:00 2001 From: Nax Date: Thu, 10 Dec 2015 20:39:16 +0100 Subject: [PATCH 2/2] Fixed typescript errors and added tests --- socketty/socketty-tests.ts | 24 ++++++++++++++++++++++++ socketty/socketty.d.ts | 8 ++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 socketty/socketty-tests.ts diff --git a/socketty/socketty-tests.ts b/socketty/socketty-tests.ts new file mode 100644 index 000000000..22809a6b0 --- /dev/null +++ b/socketty/socketty-tests.ts @@ -0,0 +1,24 @@ +/// + +/* Server */ + +var httpServer = {}; // Assume it's a real HTTP server object + +var webSocketServer = socketty.createServer(httpServer); + +webSocketServer.connection((socket: SockettySocket) => { + console.log('Client connected'); + socket.on('msg', (message?: any) => { + console.log('Client said' + message); + }); + socket.disconnect(() => { + console.log('Goodbye, client!'); + }); +}); + +/* Client */ + +socketty.connect('ws://localhost:8080', (socket: SockettySocket) => { + console.log('Connected !'); + socket.send('msg', 'Hello server!'); +}); diff --git a/socketty/socketty.d.ts b/socketty/socketty.d.ts index 9cbbeeef9..da7f82507 100644 --- a/socketty/socketty.d.ts +++ b/socketty/socketty.d.ts @@ -16,14 +16,14 @@ interface Socketty { * @param callback The callback to be run when the connection is open * @return A Socket */ - connect(url: string, callback: (SockettySocket) => void): SockettySocket; + connect(url: string, callback: (socket: SockettySocket) => void): SockettySocket; /** * Create a socketty server. * @param httpServer The HTTP server to use * @return A socketty server */ - createServer(httpServer: any): void; + createServer(httpServer: any): SockettyServer; } interface SockettySocket { @@ -32,7 +32,7 @@ interface SockettySocket { * @param action The action to listen to * @param callback A callback to be run when the action is fired */ - on(action: string, callback: (any?) => void): void; + on(action: string, callback: (message?: any) => void): void; /** * Send an action, as well as an optional message. @@ -53,5 +53,5 @@ interface SockettyServer { * Specify a callback to be run when a new socket connects to the server. * @param callback The callback */ - connection(callback: (SockettySocket) => void): void; + connection(callback: (socket: SockettySocket) => void): void; }