Search in sources :

Example 1 with WebSocketOptions

use of io.crossbar.autobahn.websocket.types.WebSocketOptions in project autobahn-java by crossbario.

the class EchoClientActivity method start.

private void start() {
    String hostname = mHostname.getText().toString();
    if (!hostname.startsWith("ws://") && !hostname.startsWith("wss://")) {
        hostname = "ws://" + hostname;
    }
    String port = mPort.getText().toString();
    String wsuri;
    if (!port.isEmpty()) {
        wsuri = hostname + ":" + port;
    } else {
        wsuri = hostname;
    }
    mStatusline.setText("Status: Connecting to " + wsuri + " ..");
    setButtonDisconnect();
    WebSocketOptions connectOptions = new WebSocketOptions();
    connectOptions.setReconnectInterval(5000);
    try {
        mConnection.connect(wsuri, new WebSocketConnectionHandler() {

            @Override
            public void onOpen() {
                mStatusline.setText("Status: Connected to " + wsuri);
                savePrefs();
                mSendMessage.setEnabled(true);
                mMessage.setEnabled(true);
            }

            @Override
            public void onMessage(String payload) {
                alert("Got echo: " + payload);
            }

            @Override
            public void onClose(int code, String reason) {
                alert("Connection lost.");
                mStatusline.setText("Status: Ready.");
                setButtonConnect();
                mSendMessage.setEnabled(false);
                mMessage.setEnabled(false);
            }
        }, connectOptions);
    } catch (WebSocketException e) {
        Log.d(TAG, e.toString());
    }
}
Also used : WebSocketOptions(io.crossbar.autobahn.websocket.types.WebSocketOptions) WebSocketException(io.crossbar.autobahn.websocket.exceptions.WebSocketException) WebSocketConnectionHandler(io.crossbar.autobahn.websocket.WebSocketConnectionHandler)

Example 2 with WebSocketOptions

use of io.crossbar.autobahn.websocket.types.WebSocketOptions in project autobahn-java by crossbario.

the class TestSuiteClientActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_suite_client);
    mWsUri = (EditText) findViewById(R.id.wsuri);
    mAgent = (EditText) findViewById(R.id.agent);
    mStatusLine = (TextView) findViewById(R.id.statusline);
    mSettings = getSharedPreferences(PREFS_NAME, 0);
    loadPrefs();
    mStart = (Button) findViewById(R.id.start);
    mStart.setOnClickListener(this);
    mOptions = new WebSocketOptions();
    mOptions.setReceiveTextMessagesRaw(true);
    mOptions.setMaxMessagePayloadSize(16 * 1024 * 1024);
    mOptions.setMaxFramePayloadSize(16 * 1024 * 1024);
}
Also used : WebSocketOptions(io.crossbar.autobahn.websocket.types.WebSocketOptions)

Example 3 with WebSocketOptions

use of io.crossbar.autobahn.websocket.types.WebSocketOptions in project autobahn-java by crossbario.

the class AndroidWebSocket method connect.

@Override
public void connect(ITransportHandler transportHandler, TransportOptions options) throws Exception {
    WebSocketOptions webSocketOptions = new WebSocketOptions();
    webSocketOptions.setAutoPingInterval(options.getAutoPingInterval());
    webSocketOptions.setAutoPingTimeout(options.getAutoPingTimeout());
    webSocketOptions.setMaxFramePayloadSize(options.getMaxFramePayloadSize());
    mConnection.connect(mUri, getSerializers(), new WebSocketConnectionHandler() {

        @Override
        public void onConnect(ConnectionResponse response) {
            LOGGER.d(String.format("Negotiated serializer=%s", response.protocol));
            try {
                mSerializer = initializeSerializer(response.protocol);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onOpen() {
            try {
                transportHandler.onConnect(AndroidWebSocket.this, mSerializer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onClose(int code, String reason) {
            switch(code) {
                case IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST:
                    transportHandler.onLeave(new CloseDetails(CloseDetails.REASON_TRANSPORT_LOST, null));
                    break;
                default:
                    transportHandler.onLeave(new CloseDetails(CloseDetails.REASON_DEFAULT, null));
            }
            LOGGER.d(String.format("Disconnected, code=%s, reasons=%s", code, reason));
            transportHandler.onDisconnect(code == 1000);
        }

        @Override
        public void onMessage(String payload) {
            try {
                transportHandler.onMessage(payload.getBytes(), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onMessage(byte[] payload, boolean isBinary) {
            try {
                transportHandler.onMessage(payload, isBinary);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, webSocketOptions, null);
}
Also used : WebSocketOptions(io.crossbar.autobahn.websocket.types.WebSocketOptions) IWebSocketConnectionHandler(io.crossbar.autobahn.websocket.interfaces.IWebSocketConnectionHandler) WebSocketConnectionHandler(io.crossbar.autobahn.websocket.WebSocketConnectionHandler) ConnectionResponse(io.crossbar.autobahn.websocket.types.ConnectionResponse) CloseDetails(io.crossbar.autobahn.wamp.types.CloseDetails)

Example 4 with WebSocketOptions

use of io.crossbar.autobahn.websocket.types.WebSocketOptions in project autobahn-java by crossbario.

the class WebSocketConnection method connect.

@Override
public void connect(String wsUri, String[] wsSubprotocols, IWebSocketConnectionHandler wsHandler, WebSocketOptions options, Map<String, String> headers) throws WebSocketException {
    // 
    if (isConnected()) {
        throw new WebSocketException("already connected");
    }
    // 
    try {
        mWsUri = new URI(wsUri);
        if (!mWsUri.getScheme().equals("ws") && !mWsUri.getScheme().equals("wss")) {
            throw new WebSocketException("unsupported scheme for WebSockets URI");
        }
        mWsScheme = mWsUri.getScheme();
        if (mWsUri.getPort() == -1) {
            if (mWsScheme.equals("ws")) {
                mWsPort = 80;
            } else {
                mWsPort = 443;
            }
        } else {
            mWsPort = mWsUri.getPort();
        }
        if (mWsUri.getHost() == null) {
            throw new WebSocketException("no host specified in WebSockets URI");
        } else {
            mWsHost = mWsUri.getHost();
        }
        if (mWsUri.getRawPath() == null || mWsUri.getRawPath().equals("")) {
            mWsPath = "/";
        } else {
            mWsPath = mWsUri.getRawPath();
        }
        if (mWsUri.getRawQuery() == null || mWsUri.getRawQuery().equals("")) {
            mWsQuery = null;
        } else {
            mWsQuery = mWsUri.getRawQuery();
        }
    } catch (URISyntaxException e) {
        throw new WebSocketException("invalid WebSockets URI");
    }
    mWsSubprotocols = wsSubprotocols;
    mWsHeaders = headers;
    mWsHandler = wsHandler;
    // make copy of options!
    mOptions = new WebSocketOptions(options);
    // set connection active
    mActive = true;
    // reset value
    onCloseCalled = false;
    // use async connector on short-lived background thread
    new WebSocketConnector().start();
}
Also used : WebSocketOptions(io.crossbar.autobahn.websocket.types.WebSocketOptions) WebSocketException(io.crossbar.autobahn.websocket.exceptions.WebSocketException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

WebSocketOptions (io.crossbar.autobahn.websocket.types.WebSocketOptions)4 WebSocketConnectionHandler (io.crossbar.autobahn.websocket.WebSocketConnectionHandler)2 WebSocketException (io.crossbar.autobahn.websocket.exceptions.WebSocketException)2 CloseDetails (io.crossbar.autobahn.wamp.types.CloseDetails)1 IWebSocketConnectionHandler (io.crossbar.autobahn.websocket.interfaces.IWebSocketConnectionHandler)1 ConnectionResponse (io.crossbar.autobahn.websocket.types.ConnectionResponse)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1