aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/jabber/ws/WSConnections.java
blob: 15fbe4e87314786b428e9f92dfe893502e2e3b6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.juick.jabber.ws;

import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 *
 * @author ugnich
 */
public class WSConnections implements Runnable {

    Selector sel;

    @Override
    public void run() {
        try {
            sel = Selector.open();
            ServerSocketChannel listensock = ServerSocketChannel.open();
            listensock.configureBlocking(false);
            listensock.socket().bind(new InetSocketAddress(8081));
            listensock.register(sel, SelectionKey.OP_ACCEPT);

            while (true) {
                sel.select();
                Iterator<SelectionKey> it = sel.selectedKeys().iterator();
                while (it.hasNext()) {
                    SelectionKey selKey = it.next();
                    it.remove();
                    ServerSocketChannel ssChannel = (ServerSocketChannel) selKey.channel();
                    SocketChannel sChannel = ssChannel.accept();
                    sChannel.configureBlocking(false);
                    sChannel.register(sel, SelectionKey.OP_READ);
                    System.out.println(sChannel.socket().getRemoteSocketAddress().toString() + " ACCEPTED");
                }
            }
        } catch (Exception e) {
            System.err.println("WSConnections: " + e);
        }
    }
}