Search in sources :

Example 1 with SocketIOClient

use of com.corundumstudio.socketio.SocketIOClient in project netty-socketio by mrniko.

the class Namespace method getRoomClients.

public Iterable<SocketIOClient> getRoomClients(String room) {
    Set<UUID> sessionIds = roomClients.get(room);
    if (sessionIds == null) {
        return Collections.emptyList();
    }
    List<SocketIOClient> result = new ArrayList<SocketIOClient>();
    for (UUID sessionId : sessionIds) {
        SocketIOClient client = allClients.get(sessionId);
        if (client != null) {
            result.add(client);
        }
    }
    return result;
}
Also used : SocketIOClient(com.corundumstudio.socketio.SocketIOClient) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 2 with SocketIOClient

use of com.corundumstudio.socketio.SocketIOClient in project netty-socketio by mrniko.

the class AuthorizeHandler method connect.

public void connect(ClientHead client) {
    Namespace ns = namespacesHub.get(Namespace.DEFAULT_NAME);
    if (!client.getNamespaces().contains(ns)) {
        Packet packet = new Packet(PacketType.MESSAGE);
        packet.setSubType(PacketType.CONNECT);
        client.send(packet);
        configuration.getStoreFactory().pubSubStore().publish(PubSubType.CONNECT, new ConnectMessage(client.getSessionId()));
        SocketIOClient nsClient = client.addNamespaceClient(ns);
        ns.onConnect(nsClient);
    }
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) SocketIOClient(com.corundumstudio.socketio.SocketIOClient) ConnectMessage(com.corundumstudio.socketio.store.pubsub.ConnectMessage) Namespace(com.corundumstudio.socketio.namespace.Namespace)

Example 3 with SocketIOClient

use of com.corundumstudio.socketio.SocketIOClient in project netty-socketio by mrniko.

the class OnEventScanner method addListener.

@Override
@SuppressWarnings("unchecked")
public void addListener(Namespace namespace, final Object object, final Method method, Annotation annot) {
    OnEvent annotation = (OnEvent) annot;
    if (annotation.value() == null || annotation.value().trim().length() == 0) {
        throw new IllegalArgumentException("OnEvent \"value\" parameter is required");
    }
    final int socketIOClientIndex = paramIndex(method, SocketIOClient.class);
    final int ackRequestIndex = paramIndex(method, AckRequest.class);
    final List<Integer> dataIndexes = dataIndexes(method);
    if (dataIndexes.size() > 1) {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        for (int index : dataIndexes) {
            Class<?> param = method.getParameterTypes()[index];
            classes.add(param);
        }
        namespace.addMultiTypeEventListener(annotation.value(), new MultiTypeEventListener() {

            @Override
            public void onData(SocketIOClient client, MultiTypeArgs data, AckRequest ackSender) {
                try {
                    Object[] args = new Object[method.getParameterTypes().length];
                    if (socketIOClientIndex != -1) {
                        args[socketIOClientIndex] = client;
                    }
                    if (ackRequestIndex != -1) {
                        args[ackRequestIndex] = ackSender;
                    }
                    int i = 0;
                    for (int index : dataIndexes) {
                        args[index] = data.get(i);
                        i++;
                    }
                    method.invoke(object, args);
                } catch (InvocationTargetException e) {
                    throw new SocketIOException(e.getCause());
                } catch (Exception e) {
                    throw new SocketIOException(e);
                }
            }
        }, classes.toArray(new Class[classes.size()]));
    } else {
        Class objectType = Void.class;
        if (!dataIndexes.isEmpty()) {
            objectType = method.getParameterTypes()[dataIndexes.iterator().next()];
        }
        namespace.addEventListener(annotation.value(), objectType, new DataListener<Object>() {

            @Override
            public void onData(SocketIOClient client, Object data, AckRequest ackSender) {
                try {
                    Object[] args = new Object[method.getParameterTypes().length];
                    if (socketIOClientIndex != -1) {
                        args[socketIOClientIndex] = client;
                    }
                    if (ackRequestIndex != -1) {
                        args[ackRequestIndex] = ackSender;
                    }
                    if (!dataIndexes.isEmpty()) {
                        int dataIndex = dataIndexes.iterator().next();
                        args[dataIndex] = data;
                    }
                    method.invoke(object, args);
                } catch (InvocationTargetException e) {
                    throw new SocketIOException(e.getCause());
                } catch (Exception e) {
                    throw new SocketIOException(e);
                }
            }
        });
    }
}
Also used : SocketIOException(com.corundumstudio.socketio.handler.SocketIOException) AckRequest(com.corundumstudio.socketio.AckRequest) ArrayList(java.util.ArrayList) MultiTypeEventListener(com.corundumstudio.socketio.listener.MultiTypeEventListener) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketIOException(com.corundumstudio.socketio.handler.SocketIOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketIOClient(com.corundumstudio.socketio.SocketIOClient) MultiTypeArgs(com.corundumstudio.socketio.MultiTypeArgs)

Example 4 with SocketIOClient

use of com.corundumstudio.socketio.SocketIOClient in project netty-socketio by mrniko.

the class NamespacesHub method getRoomClients.

public Iterable<SocketIOClient> getRoomClients(String room) {
    List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>();
    for (SocketIONamespace namespace : namespaces.values()) {
        Iterable<SocketIOClient> clients = ((Namespace) namespace).getRoomClients(room);
        allClients.add(clients);
    }
    return new CompositeIterable<SocketIOClient>(allClients);
}
Also used : SocketIOClient(com.corundumstudio.socketio.SocketIOClient) CompositeIterable(com.corundumstudio.socketio.misc.CompositeIterable) SocketIONamespace(com.corundumstudio.socketio.SocketIONamespace) ArrayList(java.util.ArrayList) CompositeIterable(com.corundumstudio.socketio.misc.CompositeIterable) SocketIONamespace(com.corundumstudio.socketio.SocketIONamespace)

Aggregations

SocketIOClient (com.corundumstudio.socketio.SocketIOClient)4 ArrayList (java.util.ArrayList)3 AckRequest (com.corundumstudio.socketio.AckRequest)1 MultiTypeArgs (com.corundumstudio.socketio.MultiTypeArgs)1 SocketIONamespace (com.corundumstudio.socketio.SocketIONamespace)1 SocketIOException (com.corundumstudio.socketio.handler.SocketIOException)1 MultiTypeEventListener (com.corundumstudio.socketio.listener.MultiTypeEventListener)1 CompositeIterable (com.corundumstudio.socketio.misc.CompositeIterable)1 Namespace (com.corundumstudio.socketio.namespace.Namespace)1 AuthPacket (com.corundumstudio.socketio.protocol.AuthPacket)1 Packet (com.corundumstudio.socketio.protocol.Packet)1 ConnectMessage (com.corundumstudio.socketio.store.pubsub.ConnectMessage)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 UUID (java.util.UUID)1