Search in sources :

Example 1 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class TestSimpleWebSocketClient method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            if (future instanceof ClientHttpFuture) {
                ClientHttpFuture f = (ClientHttpFuture) future;
                if (f.getRequestHeader("Sec-WebSocket-Accept") != null) {
                    f.updateWebSocketProtocol();
                    WebSocketFuture f2 = new WebSocketFutureImpl(session.getContext());
                    f2.write("{action: \"add-user\", username: \"火星人\"}");
                    // f2.write("{\"action\":999}");
                    session.flush(f2);
                }
                System.out.println(f.getRequestHeaders());
            } else {
                WebSocketFuture f = (WebSocketFuture) future;
                System.out.println(f.getReadText());
            }
        }
    };
    ServerConfiguration configuration = new ServerConfiguration();
    configuration.setSERVER_HOST("47.89.30.77");
    // configuration.setSERVER_HOST("120.76.222.210");
    // configuration.setSERVER_HOST("115.29.193.48");
    // configuration.setSERVER_HOST("workerman.net");
    configuration.setSERVER_PORT(7680);
    // configuration.setSERVER_PORT(30005);
    // configuration.setSERVER_PORT(29000);
    // configuration.setSERVER_PORT(8280);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.getContext().setBeatFutureFactory(new WebSocketBeatFutureFactory());
    connector.getContext().setProtocolFactory(new ClientHTTPProtocolFactory());
    connector.getContext().setSslContext(SSLUtil.initClient(true));
    SocketSession session = connector.connect();
    String url = "/web-socket-chat";
    url = "/c1020";
    HttpFuture future = new WebSocketUpgradeRequestFuture(session.getContext(), url);
    // future.setRequestURL("ws://120.76.222.210:30005/");
    // future.setResponseHeader("Host", "120.76.222.210:30005");
    // future.setResponseHeader("Pragma", "no-cache");
    // future.setResponseHeader("Cache-Control", "no-cache");
    // future.setResponseHeader("User-Agent",
    // "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    // future.setResponseHeader("Accept-Encoding", "gzip, deflate, sdch");
    // future.setResponseHeader("Accept-Language", "zh-CN,zh;q=0.8");
    // future.setRequestHeader("", "");
    session.flush(future);
    // ThreadUtil.sleep(1000);
    // WebSocketReadFuture f2 = new WebSocketReadFutureImpl();
    // f2.write("test");
    // session.flush(f2);
    ThreadUtil.sleep(999999999);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketUpgradeRequestFuture(com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) ClientHTTPProtocolFactory(com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) Future(com.generallycloud.baseio.protocol.Future) WebSocketUpgradeRequestFuture(com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture) ClientHttpFuture(com.generallycloud.baseio.codec.http11.future.ClientHttpFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) WebSocketBeatFutureFactory(com.generallycloud.baseio.codec.http11.future.WebSocketBeatFutureFactory)

Example 2 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class TestWebSocketRumpetrollServlet method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    if (future instanceof HttpFuture) {
        super.accept(session, future);
        return;
    }
    WebSocketFuture f = (WebSocketFuture) future;
    // CLOSE
    if (f.getType() == 8) {
        msgAdapter.removeClient(session);
        JSONObject o = new JSONObject();
        o.put("type", "closed");
        o.put("id", session.getSessionId());
        msgAdapter.sendMsg(o.toJSONString());
        logger.info("客户端主动关闭连接:{}", session);
    } else {
        String msg = f.getReadText();
        JSONObject o = JSON.parseObject(msg);
        String name = o.getString("name");
        if (StringUtil.isNullOrBlank(name)) {
            name = getAddress(session);
        }
        o.put("name", name);
        o.put("id", session.getSessionId());
        String type = o.getString("type");
        if ("update".equals(type)) {
            o.put("life", "1");
            o.put("authorized", "false");
            o.put("x", Double.valueOf(o.getString("x")));
            o.put("y", Double.valueOf(o.getString("x")));
            o.put("momentum", Double.valueOf(o.getString("momentum")));
            o.put("angle", Double.valueOf(o.getString("angle")));
        } else if ("message".equals(type)) {
        }
        msgAdapter.sendMsg(o.toJSONString());
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Example 3 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class WebSocketMsgAdapter method doLoop.

@Override
protected void doLoop() throws InterruptedException {
    Msg msg = msgs.poll(16, TimeUnit.MILLISECONDS);
    if (msg == null) {
        return;
    }
    synchronized (this) {
        SocketSession session = msg.session;
        if (session != null) {
            WebSocketFuture f = new WebSocketFutureImpl(session.getContext());
            f.write(msg.msg);
            session.flush(f);
            return;
        }
        for (int i = 0; i < clients.size(); i++) {
            SocketSession s = clients.get(i);
            if (s.isOpened()) {
                WebSocketFuture f = new WebSocketFutureImpl(s.getContext());
                f.write(msg.msg);
                s.flush(f);
            } else {
                removeClient(s);
                i--;
            }
        }
    }
}
Also used : SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture)

Example 4 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class TestWebSocketChatServlet method accept.

@Override
public void accept(SocketSession session, Future future) throws Exception {
    if (future instanceof HttpFuture) {
        super.accept(session, future);
        return;
    }
    WebSocketFuture f = (WebSocketFuture) future;
    // CLOSE
    if (f.isCloseFrame()) {
        if (!msgAdapter.removeClient(session)) {
            return;
        }
        JSONObject obj = new JSONObject();
        obj.put("username", session.getAttribute("username"));
        obj.put("numUsers", msgAdapter.getClientSize());
        obj.put("action", "user-left");
        String msg1 = obj.toJSONString();
        msgAdapter.sendMsg(msg1);
    } else {
        String msg = f.getReadText();
        JSONObject obj = JSON.parseObject(msg);
        String action = obj.getString("action");
        if ("new-message".equals(action)) {
            String owner = (String) session.getAttribute("username");
            String message = obj.getString("message");
            if (message.charAt(0) == '@') {
                int nIndex = message.indexOf(' ');
                if (nIndex > 1) {
                    String username = message.substring(1, nIndex);
                    SocketSession s = msgAdapter.getSession(username);
                    if (s == null) {
                        obj.put("message", "用户不存在或者已离线");
                        obj.put("username", owner);
                        msgAdapter.sendMsg(session, obj.toJSONString());
                        return;
                    }
                    obj.put("username", owner);
                    msgAdapter.sendMsg(session, obj.toJSONString());
                    obj.put("username", owner + "@你");
                    obj.put("message", message.substring(nIndex));
                    msgAdapter.sendMsg(s, obj.toJSONString());
                    return;
                }
            }
            obj.put("username", owner);
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("add-user".equals(action)) {
            String username = (String) session.getAttribute("username");
            if (username != null) {
                return;
            }
            username = obj.getString("username");
            if (StringUtil.isNullOrBlank(username)) {
                return;
            }
            msgAdapter.addClient(username, session);
            session.setAttribute("username", username);
            obj.put("numUsers", msgAdapter.getClientSize());
            obj.put("action", "login");
            msgAdapter.sendMsg(session, obj.toJSONString());
            obj.put("username", username);
            obj.put("action", "user-joined");
            msgAdapter.sendMsg(obj.toJSONString());
            obj.put("action", "new-message");
            obj.put("username", "系统消息");
            obj.put("message", "欢迎加入QQ群讨论java io相关技术:540637859,@某人可以单独向他发送消息。");
            msgAdapter.sendMsg(session, obj.toJSONString());
        } else if ("typing".equals(action)) {
            obj.put("username", session.getAttribute("username"));
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("stop-typing".equals(action)) {
            obj.put("username", session.getAttribute("username"));
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else if ("disconnect".equals(action)) {
            msgAdapter.removeClient(session);
            obj.put("username", session.getAttribute("username"));
            obj.put("numUsers", msgAdapter.getClientSize());
            obj.put("action", "user-left");
            String msg1 = obj.toJSONString();
            msgAdapter.sendMsg(msg1);
        } else {
            f.write("no action matched:" + action);
            session.flush(f);
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture) HttpFuture(com.generallycloud.baseio.codec.http11.future.HttpFuture)

Example 5 with WebSocketFuture

use of com.generallycloud.baseio.codec.http11.future.WebSocketFuture in project baseio by generallycloud.

the class TestWebSocketRumpetrollServlet method doAccept.

@Override
protected void doAccept(HttpSession session, HttpFuture future) throws Exception {
    future.updateWebSocketProtocol();
    session.flush(future);
    msgAdapter.addClient(getAddress(session.getIoSession()), session.getIoSession());
    SocketSession ioSession = session.getIoSession();
    JSONObject o = new JSONObject();
    o.put("type", "welcome");
    o.put("id", ioSession.getSessionId());
    WebSocketFuture f = new WebSocketFutureImpl(ioSession.getContext());
    f.write(o.toJSONString());
    session.flush(f);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) WebSocketFutureImpl(com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl) WebSocketFuture(com.generallycloud.baseio.codec.http11.future.WebSocketFuture)

Aggregations

WebSocketFuture (com.generallycloud.baseio.codec.http11.future.WebSocketFuture)6 SocketSession (com.generallycloud.baseio.component.SocketSession)4 JSONObject (com.alibaba.fastjson.JSONObject)3 HttpFuture (com.generallycloud.baseio.codec.http11.future.HttpFuture)3 WebSocketFutureImpl (com.generallycloud.baseio.codec.http11.future.WebSocketFutureImpl)3 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 ClientHTTPProtocolFactory (com.generallycloud.baseio.codec.http11.ClientHTTPProtocolFactory)1 ClientHttpFuture (com.generallycloud.baseio.codec.http11.future.ClientHttpFuture)1 WebSocketBeatFutureFactory (com.generallycloud.baseio.codec.http11.future.WebSocketBeatFutureFactory)1 WebSocketUpgradeRequestFuture (com.generallycloud.baseio.codec.http11.future.WebSocketUpgradeRequestFuture)1 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)1 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)1 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)1 Future (com.generallycloud.baseio.protocol.Future)1