From 58fb00ab113c8e40d3975a791dbfd12acd911981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Mon, 13 Mar 2017 19:41:52 +0900 Subject: [PATCH 1/4] class Socket extends EventEmitter https://github.com/socketio/socket.io/blob/master/lib/socket.js#L81 --- socket.io/index.d.ts | 60 +------------------------------------------- 1 file changed, 1 insertion(+), 59 deletions(-) diff --git a/socket.io/index.d.ts b/socket.io/index.d.ts index 2333dc8fe2..f6c94363ec 100644 --- a/socket.io/index.d.ts +++ b/socket.io/index.d.ts @@ -472,7 +472,7 @@ declare namespace SocketIO { * as we have a problem with the emit() event (as it's overridden with a * different return) */ - interface Socket { + interface Socket extends NodeJS.EventEmitter{ /** * The namespace that this socket is for @@ -613,17 +613,6 @@ declare namespace SocketIO { */ broadcast: Socket; - /** - * Emits an event to this client. If the 'broadcast' flag was set, this will - * emit to all other clients, except for this one - * @param event The event that we want to emit - * @param args Any number of optional arguments to pass with the event. If the - * last argument is a function, it will be called as an ack. The ack should - * take whatever data was sent with the packet - * @return This Socket - */ - emit( event: string, ...args: any[]): Socket; - /** * Targets a room when broadcasting * @param room The name of the room that we're targeting @@ -677,53 +666,6 @@ declare namespace SocketIO { */ disconnect( close?: boolean ): Socket; - /** - * Adds a listener for a particular event. Calling multiple times will add - * multiple listeners - * @param event The event that we're listening for - * @param fn The function to call when we get the event. Parameters depend on the - * event in question - * @return This Socket - */ - on( event: string, fn: Function ): Socket; - - /** - * @see on( event, fn ) - */ - addListener( event: string, fn: Function ): Socket; - - /** - * Adds a listener for a particular event that will be invoked - * a single time before being automatically removed - * @param event The event that we're listening for - * @param fn The function to call when we get the event. Parameters depend on - * the event in question - * @return This Socket - */ - once( event: string, fn: Function ): Socket; - - /** - * Removes a listener for a particular type of event. This will either - * remove a specific listener, or all listeners for this type of event - * @param event The event that we want to remove the listener of - * @param fn The function to remove, or null if we want to remove all functions - * @return This Socket - */ - removeListener( event: string, fn?: Function ): Socket; - - /** - * Removes all event listeners on this object - * @return This Socket - */ - removeAllListeners( event?: string ): Socket; - - /** - * Sets the maximum number of listeners this instance can have - * @param n The max number of listeners we can add to this emitter - * @return This Socket - */ - setMaxListeners( n: number ): Socket; - /** * Returns all the callbacks for a particular event * @param event The event that we're looking for the callbacks of From f133fc0c1e9f34236a2caa0c79570b1bd25c7895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Mon, 13 Mar 2017 19:46:15 +0900 Subject: [PATCH 2/4] Client#conn, Socket#conn is an instance of engine.Socket, including properties "request" and "upgraded". https://github.com/socketio/socket.io/blob/master/docs/API.md#socketconn https://github.com/socketio/socket.io/blob/master/docs/API.md#clientconn https://github.com/socketio/engine.io#properties-2 --- socket.io/index.d.ts | 86 ++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/socket.io/index.d.ts b/socket.io/index.d.ts index f6c94363ec..8e2ec2aade 100644 --- a/socket.io/index.d.ts +++ b/socket.io/index.d.ts @@ -509,28 +509,7 @@ declare namespace SocketIO { /** * The underlying Engine.io Socket instance */ - conn: { - - /** - * The ID for this socket - matches Client.id - */ - id: string; - - /** - * The Engine.io Server for this socket - */ - server: any; - - /** - * The ready state for the client. Either 'opening', 'open', 'closing', or 'closed' - */ - readyState: string; - - /** - * The remote IP for this connection - */ - remoteAddress: string; - }; + conn: EngineSocket; /** * The list of rooms that this Socket is currently in, where @@ -751,28 +730,7 @@ declare namespace SocketIO { /** * The underlying Engine.io Socket instance */ - conn: { - - /** - * The ID for this socket - matches Client.id - */ - id: string; - - /** - * The Engine.io Server for this socket - */ - server: any; - - /** - * The ready state for the client. Either 'opening', 'open', 'closing', or 'closed' - */ - readyState: string; - - /** - * The remote IP for this connection - */ - remoteAddress: string; - }; + conn: EngineSocket; /** * The ID for this client. Regenerated at every connection @@ -797,4 +755,44 @@ declare namespace SocketIO { */ nsps: {[nsp: string]: Socket}; } + + /** + * A reference to the underlying engine.io Socket connection. + */ + interface EngineSocket extends NodeJS.EventEmitter { + /** + * The ID for this socket - matches Client.id + */ + id: string; + + /** + * The Engine.io Server for this socket + */ + server: any; + + /** + * The ready state for the client. Either 'opening', 'open', 'closing', or 'closed' + */ + readyState: string; + + /** + * The remote IP for this connection + */ + remoteAddress: string; + + /** + * whether the transport has been upgraded + */ + upgraded: boolean; + + /** + * (http.IncomingMessage): request that originated the Socket + */ + request: any; + + /** + * (Transport): transport reference + */ + transport: any; + } } From fe5c8722e7c313ee4822fc2fbbf9a1f02d609ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Mon, 13 Mar 2017 19:58:55 +0900 Subject: [PATCH 3/4] Add a test. --- socket.io/socket.io-tests.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/socket.io/socket.io-tests.ts b/socket.io/socket.io-tests.ts index ea3d97e21f..23c4a6d996 100644 --- a/socket.io/socket.io-tests.ts +++ b/socket.io/socket.io-tests.ts @@ -144,6 +144,22 @@ function testUsingItJustAsACrossBrowserWebSocket() { }); } +function testSocketConnection() { + var io = socketIO.listen(80); + + io.sockets.on('connection', function (socket) { + console.log(socket.client.conn === socket.conn); + console.log(socket.client.request.httpVersion); + console.log(socket.conn.id); + console.log(socket.conn.upgraded); + console.log(socket.conn.readyState); + + socket.on('packet', function(message :string, ping :string){ + console.log(message, ping); + });; + }); +} + function testClosingServerWithCallback() { var io = socketIO.listen(80); io.close(function() { From 7713b5342f34ae611da631117143a627050f05d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Mon, 13 Mar 2017 20:08:30 +0900 Subject: [PATCH 4/4] Add my credit. --- socket.io/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/socket.io/index.d.ts b/socket.io/index.d.ts index 8e2ec2aade..9eafcec6a1 100644 --- a/socket.io/index.d.ts +++ b/socket.io/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for socket.io 1.4.4 // Project: http://socket.io/ -// Definitions by: PROGRE , Damian Connolly , Florent Poujol +// Definitions by: PROGRE , Damian Connolly , Florent Poujol , KentarouTakeda // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped ///