Skip to content

Added methods for broadcasting messages (excluding sockets), custom data storing inside a socket and checking the room for emptiness #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ public void broadcast(Packet<?> packet, String[] rooms) throws IllegalArgumentEx
* @throws IllegalArgumentException If socket is null.
*/
public abstract String[] listClientRooms(SocketIoSocket socket) throws IllegalArgumentException;

/**
* Check whether room is empty
*
* @param room Room name to list sockets in.
* @return boolean whether room is empty
* @throws IllegalArgumentException If room is null.
*/
public abstract boolean isEmptyRoom(String room) throws IllegalArgumentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,14 @@ public String[] listClientRooms(SocketIoSocket socket) throws IllegalArgumentExc
return new String[0];
}
}

@Override
public boolean isEmptyRoom(String room) throws IllegalArgumentException {
if (room == null) {
throw new IllegalArgumentException("room must not be null.");
}

Set<SocketIoSocket> socketsInRoom = mRoomSockets.get(room);
return socketsInRoom == null || socketsInRoom.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,17 @@ public final void broadcast(String room, String event, Object... args) throws Il
*/
public abstract void broadcast(String[] rooms, String event, Object[] args) throws IllegalArgumentException;

/**
* Broadcast a message to all clients in this namespace that
* have joined specified rooms. Optionally, specify sockets to exclude from sending.
*
* @param rooms Rooms to send message to.
* @param event Name of event to raise on remote client.
* @param socketsExcluded List of sockets to exclude from sending or null.
* @param args Array of arguments to send. Supported types are: JSONObject, JSONArray, null
* @throws IllegalArgumentException If event is null or argument is not of supported type.
*/
public abstract void broadcast(String[] rooms, String event, SocketIoSocket[] socketsExcluded, Object[] args) throws IllegalArgumentException;

abstract Map<String, SocketIoSocket> getConnectedSockets();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import io.socket.engineio.server.Emitter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

final class SocketIoNamespaceGroupImpl extends SocketIoNamespace {
Expand All @@ -24,6 +22,13 @@ public void broadcast(String[] rooms, String event, Object[] args) throws Illega
}
}

@Override
public void broadcast(String[] rooms, String event, SocketIoSocket[] socketsExcluded, Object[] args) throws IllegalArgumentException {
for (SocketIoNamespaceImpl namespace : mChildNamespaces) {
namespace.broadcast(rooms, event, socketsExcluded, args);
}
}

@Override
Map<String, SocketIoSocket> getConnectedSockets() {
final Map<String, SocketIoSocket> sockets = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import io.socket.socketio.server.parser.Packet;
import io.socket.socketio.server.parser.Parser;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -32,6 +34,24 @@ public void broadcast(String[] rooms, String event, Object[] args) throws Illega
mAdapter.broadcast(packet, rooms);
}

@Override
public void broadcast(String[] rooms, String event, SocketIoSocket[] socketsExcluded, Object[] args) throws IllegalArgumentException {
if (event == null) {
throw new IllegalArgumentException("event cannot be null.");
}

final Packet packet = PacketUtils.createDataPacket(Parser.EVENT, event, args);

String[] socketsExcludedIds = socketsExcluded == null ? null :
Arrays.stream(socketsExcluded)
.filter(Objects::nonNull)
.map(SocketIoSocket::getId)
.distinct()
.toArray(String[]::new);

mAdapter.broadcast(packet, rooms, socketsExcludedIds);
}

@Override
Map<String, SocketIoSocket> getConnectedSockets() {
return mConnectedSockets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public interface ReceivedByLocalAcknowledgementCallback {
private final HashMap<Integer, ReceivedByRemoteAcknowledgementCallback> mAcknowledgementCallbacks = new HashMap<>();

private boolean mConnected;
private Object customData;

SocketIoSocket(SocketIoNamespaceImpl namespace, SocketIoClient client, Object connectData) {
mNamespace = namespace;
Expand Down Expand Up @@ -184,6 +185,33 @@ public void broadcast(String[] rooms, String event, Object[] args) throws Illega
mAdapter.broadcast(packet, rooms, new String[] { getId() });
}

/**
* Broadcast a message to all clients in this namespace that
* have joined specified rooms. Optionally, specify sockets to exclude from sending.
*
* @param rooms Rooms to send message to.
* @param event Name of event to raise on remote client.
* @param socketsExcluded List of sockets to exclude from sending or null.
* @param args Array of arguments to send. Supported types are: JSONObject, JSONArray, null.
* @throws IllegalArgumentException If argument is not of supported type or socketsExcluded is null.
*/
public void broadcast(String[] rooms, String event, SocketIoSocket[] socketsExcluded, Object[] args) throws IllegalArgumentException {
if (event == null) {
throw new IllegalArgumentException("event cannot be null.");
}

final Packet<?> packet = PacketUtils.createDataPacket(Parser.EVENT, event, args);

String[] socketsExcludedIds = socketsExcluded == null ? null :
Arrays.stream(socketsExcluded)
.filter(Objects::nonNull)
.map(SocketIoSocket::getId)
.distinct()
.toArray(String[]::new);

mAdapter.broadcast(packet, rooms, socketsExcludedIds);
}

/**
* Send data to remote client.
*
Expand Down Expand Up @@ -270,6 +298,20 @@ public synchronized void leaveAllRooms() {
mRooms.clear();
}

/**
* Set customData object.
*/
public void setCustomData(Object customData) {
this.customData = customData;
}

/**
* Get customData object.
*/
public Object getCustomData() {
return this.customData;
}

/**
* Register listener for all user events.
*
Expand Down