socket.io-adapter
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -0,1 +1,10 @@ | ||
# [2.2.0](https://github.com/socketio/socket.io-adapter/compare/2.1.0...2.2.0) (2021-02-27) | ||
### Features | ||
* add some utility methods ([1c9827e](https://github.com/socketio/socket.io-adapter/commit/1c9827ec1136e24094295907efaf4d4e6c2fef2f)) | ||
* allow excluding all sockets in a room ([#66](https://github.com/socketio/socket.io-adapter/issues/66)) ([985bb41](https://github.com/socketio/socket.io-adapter/commit/985bb41fa2c04f17f1cf3a17c14ab9acde8947f7)) | ||
# [2.1.0](https://github.com/socketio/socket.io-adapter/compare/2.0.3...2.1.0) (2021-01-15) | ||
@@ -2,0 +11,0 @@ |
@@ -83,2 +83,31 @@ /// <reference types="node" /> | ||
socketRooms(id: SocketId): Set<Room> | undefined; | ||
/** | ||
* Returns the matching socket instances | ||
* | ||
* @param opts - the filters to apply | ||
*/ | ||
fetchSockets(opts: BroadcastOptions): Promise<any[]>; | ||
/** | ||
* Makes the matching socket instances join the specified rooms | ||
* | ||
* @param opts - the filters to apply | ||
* @param rooms - the rooms to join | ||
*/ | ||
addSockets(opts: BroadcastOptions, rooms: Room[]): void; | ||
/** | ||
* Makes the matching socket instances leave the specified rooms | ||
* | ||
* @param opts - the filters to apply | ||
* @param rooms - the rooms to leave | ||
*/ | ||
delSockets(opts: BroadcastOptions, rooms: Room[]): void; | ||
/** | ||
* Makes the matching socket instances disconnect | ||
* | ||
* @param opts - the filters to apply | ||
* @param close - whether to close the underlying connection | ||
*/ | ||
disconnectSockets(opts: BroadcastOptions, close: boolean): void; | ||
private apply; | ||
private computeExceptSids; | ||
} |
@@ -100,4 +100,2 @@ "use strict"; | ||
broadcast(packet, opts) { | ||
const rooms = opts.rooms; | ||
const except = opts.except || new Set(); | ||
const flags = opts.flags || {}; | ||
@@ -109,6 +107,78 @@ const packetOpts = { | ||
}; | ||
const ids = new Set(); | ||
packet.nsp = this.nsp.name; | ||
const encodedPackets = this.encoder.encode(packet); | ||
this.apply(opts, socket => { | ||
socket.packet(encodedPackets, packetOpts); | ||
}); | ||
} | ||
/** | ||
* Gets a list of sockets by sid. | ||
* | ||
* @param {Set<Room>} rooms the explicit set of rooms to check. | ||
*/ | ||
sockets(rooms) { | ||
const sids = new Set(); | ||
this.apply({ rooms }, socket => { | ||
sids.add(socket.id); | ||
}); | ||
return Promise.resolve(sids); | ||
} | ||
/** | ||
* Gets the list of rooms a given socket has joined. | ||
* | ||
* @param {SocketId} id the socket id | ||
*/ | ||
socketRooms(id) { | ||
return this.sids.get(id); | ||
} | ||
/** | ||
* Returns the matching socket instances | ||
* | ||
* @param opts - the filters to apply | ||
*/ | ||
fetchSockets(opts) { | ||
const sockets = []; | ||
this.apply(opts, socket => { | ||
sockets.push(socket); | ||
}); | ||
return Promise.resolve(sockets); | ||
} | ||
/** | ||
* Makes the matching socket instances join the specified rooms | ||
* | ||
* @param opts - the filters to apply | ||
* @param rooms - the rooms to join | ||
*/ | ||
addSockets(opts, rooms) { | ||
this.apply(opts, socket => { | ||
socket.join(rooms); | ||
}); | ||
} | ||
/** | ||
* Makes the matching socket instances leave the specified rooms | ||
* | ||
* @param opts - the filters to apply | ||
* @param rooms - the rooms to leave | ||
*/ | ||
delSockets(opts, rooms) { | ||
this.apply(opts, socket => { | ||
rooms.forEach(room => socket.leave(room)); | ||
}); | ||
} | ||
/** | ||
* Makes the matching socket instances disconnect | ||
* | ||
* @param opts - the filters to apply | ||
* @param close - whether to close the underlying connection | ||
*/ | ||
disconnectSockets(opts, close) { | ||
this.apply(opts, socket => { | ||
socket.disconnect(close); | ||
}); | ||
} | ||
apply(opts, callback) { | ||
const rooms = opts.rooms; | ||
const except = this.computeExceptSids(opts.except); | ||
if (rooms.size) { | ||
const ids = new Set(); | ||
for (const room of rooms) { | ||
@@ -122,3 +192,3 @@ if (!this.rooms.has(room)) | ||
if (socket) { | ||
socket.packet(encodedPackets, packetOpts); | ||
callback(socket); | ||
ids.add(id); | ||
@@ -135,41 +205,18 @@ } | ||
if (socket) | ||
socket.packet(encodedPackets, packetOpts); | ||
callback(socket); | ||
} | ||
} | ||
} | ||
/** | ||
* Gets a list of sockets by sid. | ||
* | ||
* @param {Set<Room>} rooms the explicit set of rooms to check. | ||
*/ | ||
sockets(rooms) { | ||
const sids = new Set(); | ||
if (rooms.size) { | ||
for (const room of rooms) { | ||
if (!this.rooms.has(room)) | ||
continue; | ||
for (const id of this.rooms.get(room)) { | ||
if (this.nsp.sockets.has(id)) { | ||
sids.add(id); | ||
} | ||
computeExceptSids(exceptRooms) { | ||
const exceptSids = new Set(); | ||
if (exceptRooms && exceptRooms.size > 0) { | ||
for (const room of exceptRooms) { | ||
if (this.rooms.has(room)) { | ||
this.rooms.get(room).forEach(sid => exceptSids.add(sid)); | ||
} | ||
} | ||
} | ||
else { | ||
for (const [id] of this.sids) { | ||
if (this.nsp.sockets.has(id)) | ||
sids.add(id); | ||
} | ||
} | ||
return Promise.resolve(sids); | ||
return exceptSids; | ||
} | ||
/** | ||
* Gets the list of rooms a given socket has joined. | ||
* | ||
* @param {SocketId} id the socket id | ||
*/ | ||
socketRooms(id) { | ||
return this.sids.get(id); | ||
} | ||
} | ||
exports.Adapter = Adapter; |
{ | ||
"name": "socket.io-adapter", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14176
330