Search in sources :

Example 1 with WebSocket

use of com.codename1.io.websocket.WebSocket in project CodenameOne by codenameone.

the class WebSocketsSample method start.

public void start() {
    if (false) {
        testTimeouts();
        return;
    }
    System.out.println("About to start socket");
    sock = new WebSocket(SERVER_URL) {

        @Override
        protected void onOpen() {
            System.out.println("In onOpen");
            System.out.println("Ready state: " + sock.getReadyState());
        }

        @Override
        protected void onClose(int statusCode, String reason) {
            System.out.println("Closing: " + sock.getReadyState());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    showLogin();
                }
            });
        }

        @Override
        protected void onMessage(final String message) {
            System.out.println("Received message " + message);
            System.out.println("Ready state: " + sock.getReadyState());
            Display.getInstance().callSerially(new Runnable() {

                public void run() {
                    if (chatContainer == null) {
                        return;
                    }
                    SpanLabel label = new SpanLabel();
                    label.setText(message);
                    chatContainer.addComponent(label);
                    chatContainer.animateHierarchy(100);
                }
            });
        }

        @Override
        protected void onError(Exception ex) {
            if (sock == null) {
                System.out.println("Error while socket is null: " + ex.getMessage());
            } else {
                System.out.println("Ready state: " + sock.getReadyState());
                System.out.println("in onError " + ex.getMessage());
            // Log.e(ex);
            }
        }

        @Override
        protected void onMessage(byte[] message) {
            System.out.println("Received bytes " + message.length);
            System.out.println(Arrays.toString(message));
        }
    }.autoReconnect(10000);
    System.out.println("Sending connect");
    System.out.println("Ready State: " + sock.getReadyState());
    sock.connect();
    try {
        sock.send("Test Message");
    } catch (Throwable t) {
        t.printStackTrace();
    }
    showLogin();
}
Also used : SpanLabel(com.codename1.components.SpanLabel) IOException(java.io.IOException) WebSocket(com.codename1.io.websocket.WebSocket)

Example 2 with WebSocket

use of com.codename1.io.websocket.WebSocket in project CodenameOne by codenameone.

the class WebSocketReconnectTest method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    String uniqueId = "1234";
    Form hi = new Form("WebSocket Test", BoxLayout.y());
    hi.getToolbar().addCommandToRightBar("Start Test", null, evt -> {
        hi.removeAll();
        String url = "wss://weblite.ca/ws/cn1-websocket-demo/chat";
        if (!"HTML5".equals(CN.getPlatformName())) {
            url = "wss://chat.mydissent.net/wsMsg";
        }
        WebSocket.setDebugLoggingEnabled(true);
        final WebSocket sock = new WebSocket(url) {

            @Override
            protected void onOpen() {
                Log.p("WebSocket onOpen - Sending uniqueId: " + uniqueId);
                hi.add("WebSocket onOpen - Sending uniqueId: " + uniqueId);
                hi.animateLayout(400);
                send(uniqueId);
            }

            @Override
            protected void onClose(int statusCode, String reason) {
                Log.p("WebSocket onClose");
                hi.add("WebSocket onClose");
                hi.animateLayout(400);
            }

            @Override
            protected void onMessage(String message) {
                Log.p("WebSocket onMessage: " + message);
                hi.add(new SpanLabel("WebSocket onMessage: " + message));
                hi.animateLayout(400);
            }

            @Override
            protected void onMessage(byte[] message) {
                Log.p("WebSocket onMessage (byte[]): " + message.toString());
            }

            @Override
            protected void onError(Exception ex) {
                Log.e(ex);
                hi.add(new SpanLabel("WebSocket onError:\n" + ex.getMessage()));
            }
        };
        sock.connect();
        sock.autoReconnect(5000);
        UITimer.timer(5000, false, hi, () -> {
            sock.close();
        });
    });
    hi.show();
}
Also used : Form(com.codename1.ui.Form) SpanLabel(com.codename1.components.SpanLabel) IOException(java.io.IOException) WebSocket(com.codename1.io.websocket.WebSocket)

Example 3 with WebSocket

use of com.codename1.io.websocket.WebSocket in project CodenameOne by codenameone.

the class WebSocketsSample method testTimeouts.

private void testTimeouts() {
    WebSocket ws = new WebSocket("http://10.0.1.77/ws") {

        @Override
        protected void onError(Exception ex) {
            Log.e(ex);
        }

        @Override
        protected void onOpen() {
            Log.p("Opened");
        }

        @Override
        protected void onClose(int statusCode, String reason) {
            Log.p("Closed " + statusCode + ", " + reason);
        }

        @Override
        protected void onMessage(String message) {
        }

        @Override
        protected void onMessage(byte[] message) {
        }
    };
    System.out.println("Trying to connect");
    ws.connect(5000);
    Form f = new Form("Hello");
    f.add(new Label("World"));
    f.show();
}
Also used : Form(com.codename1.ui.Form) SpanLabel(com.codename1.components.SpanLabel) Label(com.codename1.ui.Label) IOException(java.io.IOException) WebSocket(com.codename1.io.websocket.WebSocket)

Aggregations

SpanLabel (com.codename1.components.SpanLabel)3 WebSocket (com.codename1.io.websocket.WebSocket)3 IOException (java.io.IOException)3 Form (com.codename1.ui.Form)2 Label (com.codename1.ui.Label)1