Search in sources :

Example 36 with Packet

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

the class AuthorizeHandler method authorize.

private boolean authorize(ChannelHandlerContext ctx, Channel channel, String origin, Map<String, List<String>> params, FullHttpRequest req) throws IOException {
    Map<String, List<String>> headers = new HashMap<String, List<String>>(req.headers().names().size());
    for (String name : req.headers().names()) {
        List<String> values = req.headers().getAll(name);
        headers.put(name, values);
    }
    HandshakeData data = new HandshakeData(req.headers(), params, (InetSocketAddress) channel.remoteAddress(), req.uri(), origin != null && !origin.equalsIgnoreCase("null"));
    boolean result = false;
    try {
        result = configuration.getAuthorizationListener().isAuthorized(data);
    } catch (Exception e) {
        log.error("Authorization error", e);
    }
    if (!result) {
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        log.debug("Handshake unauthorized, query params: {} headers: {}", params, headers);
        return false;
    }
    UUID sessionId = this.generateOrGetSessionIdFromRequest(req.headers());
    List<String> transportValue = params.get("transport");
    if (transportValue == null) {
        log.error("Got no transports for request {}", req.uri());
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        return false;
    }
    Transport transport = Transport.byName(transportValue.get(0));
    if (!configuration.getTransports().contains(transport)) {
        Map<String, Object> errorData = new HashMap<String, Object>();
        errorData.put("code", 0);
        errorData.put("message", "Transport unknown");
        channel.attr(EncoderHandler.ORIGIN).set(origin);
        channel.writeAndFlush(new HttpErrorMessage(errorData));
        return false;
    }
    ClientHead client = new ClientHead(sessionId, ackManager, disconnectable, storeFactory, data, clientsBox, transport, disconnectScheduler, configuration);
    channel.attr(ClientHead.CLIENT).set(client);
    clientsBox.addClient(client);
    String[] transports = {};
    if (configuration.getTransports().contains(Transport.WEBSOCKET)) {
        transports = new String[] { "websocket" };
    }
    AuthPacket authPacket = new AuthPacket(sessionId, transports, configuration.getPingInterval(), configuration.getPingTimeout());
    Packet packet = new Packet(PacketType.OPEN);
    packet.setData(authPacket);
    client.send(packet);
    client.schedulePingTimeout();
    log.debug("Handshake authorized for sessionId: {}, query params: {} headers: {}", sessionId, params, headers);
    return true;
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HashMap(java.util.HashMap) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) HttpErrorMessage(com.corundumstudio.socketio.messages.HttpErrorMessage) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HandshakeData(com.corundumstudio.socketio.HandshakeData) List(java.util.List) UUID(java.util.UUID) Transport(com.corundumstudio.socketio.Transport)

Example 37 with Packet

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

the class NamespaceClient method disconnect.

@Override
public void disconnect() {
    Packet packet = new Packet(PacketType.MESSAGE);
    packet.setSubType(PacketType.DISCONNECT);
    send(packet);
//        onDisconnect();
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet)

Example 38 with Packet

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

the class AckRequest method sendAckData.

/**
     * Send ack data to client.
     * Can be invoked only once during {@link DataListener#onData}
     * method invocation.
     *
     * @param objs - ack data object list
     */
public void sendAckData(List<Object> objs) {
    if (!isAckRequested() || !sended.compareAndSet(false, true)) {
        return;
    }
    Packet ackPacket = new Packet(PacketType.MESSAGE);
    ackPacket.setSubType(PacketType.ACK);
    ackPacket.setAckId(originalPacket.getAckId());
    ackPacket.setData(objs);
    client.send(ackPacket);
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet)

Example 39 with Packet

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

the class BroadcastOperations method sendEvent.

@Override
public void sendEvent(String name, Object... data) {
    Packet packet = new Packet(PacketType.MESSAGE);
    packet.setSubType(PacketType.EVENT);
    packet.setName(name);
    packet.setData(Arrays.asList(data));
    send(packet);
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet)

Aggregations

Packet (com.corundumstudio.socketio.protocol.Packet)39 Test (org.junit.Test)27 ByteBuf (io.netty.buffer.ByteBuf)15 Namespace (com.corundumstudio.socketio.namespace.Namespace)2 AuthPacket (com.corundumstudio.socketio.protocol.AuthPacket)2 HandshakeData (com.corundumstudio.socketio.HandshakeData)1 SocketIOClient (com.corundumstudio.socketio.SocketIOClient)1 Transport (com.corundumstudio.socketio.Transport)1 HttpErrorMessage (com.corundumstudio.socketio.messages.HttpErrorMessage)1 JacksonJsonSupport (com.corundumstudio.socketio.protocol.JacksonJsonSupport)1 PacketDecoder (com.corundumstudio.socketio.protocol.PacketDecoder)1 ConnectMessage (com.corundumstudio.socketio.store.pubsub.ConnectMessage)1 NamespaceClient (com.corundumstudio.socketio.transport.NamespaceClient)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)1