[@types/socket.io] fix: socket.use (#24574)

This commit is contained in:
Maciej Sopyło
2018-05-14 10:50:50 +02:00
parent 9028a63234
commit 27763ecbb2
2 changed files with 31 additions and 0 deletions

View File

@@ -474,6 +474,21 @@ declare namespace SocketIO {
compress( compress: boolean ): Namespace;
}
interface Packet extends Array<any> {
/**
* Event name
*/
[0]: string;
/**
* Packet data
*/
[1]: any;
/**
* Ack function
*/
[2]: (...args: any[]) => void;
}
/**
* The socket, which handles our connection for a namespace. NOTE: while
* we technically extend NodeJS.EventEmitter, we're not putting it here
@@ -612,6 +627,13 @@ declare namespace SocketIO {
*/
in( room: string ): Socket;
/**
* Registers a middleware, which is a function that gets executed for every incoming Packet and receives as parameter the packet and a function to optionally defer execution to the next registered middleware.
*
* Errors passed to middleware callbacks are sent as special error packets to clients.
*/
use( fn: ( packet: Packet, next: (err?: any) => void ) => void ): Socket;
/**
* Sends a 'message' event
* @see emit( event, ...args )

View File

@@ -180,3 +180,12 @@ function testVolatileServerMessages() {
var io = socketIO.listen(80);
io.volatile.emit('volatile', 'Lost data');
}
function testSocketUse() {
var io = socketIO.listen(80);
io.on('connection', (socket) => {
socket.use((packet, next) => {
console.log(packet);
});
});
}