Search in sources :

Example 1 with HandshakeFuture

use of org.wso2.transport.http.netty.contract.websocket.HandshakeFuture in project ballerina by ballerina-lang.

the class Start method execute.

@Override
public void execute(Context context) {
    HttpWsConnectorFactory connectorFactory = HttpUtil.createHttpWsConnectionFactory();
    Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
    Object configs = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS);
    if (configs == null || !(configs instanceof WsClientConnectorConfig)) {
        throw new BallerinaConnectorException("Initialize the service before starting it");
    }
    WebSocketClientConnector clientConnector = connectorFactory.createWsClientConnector((WsClientConnectorConfig) configs);
    WebSocketClientConnectorListener clientConnectorListener = new WebSocketClientConnectorListener();
    Object serviceConfig = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG);
    if (serviceConfig == null || !(serviceConfig instanceof WebSocketService)) {
        throw new BallerinaConnectorException("Initialize the service before starting it");
    }
    WebSocketService wsService = (WebSocketService) serviceConfig;
    HandshakeFuture handshakeFuture = clientConnector.connect(clientConnectorListener);
    handshakeFuture.setHandshakeListener(new WsHandshakeListener(context, wsService, clientConnectorListener));
    context.setReturnValues();
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) WsClientConnectorConfig(org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig) HandshakeFuture(org.wso2.transport.http.netty.contract.websocket.HandshakeFuture) WebSocketClientConnector(org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector) WebSocketClientConnectorListener(org.ballerinalang.net.http.WebSocketClientConnectorListener) WebSocketService(org.ballerinalang.net.http.WebSocketService) HttpWsConnectorFactory(org.wso2.transport.http.netty.contract.HttpWsConnectorFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 2 with HandshakeFuture

use of org.wso2.transport.http.netty.contract.websocket.HandshakeFuture in project ballerina by ballerina-lang.

the class WebSocketUtil method handleHandshake.

public static void handleHandshake(WebSocketService wsService, HttpHeaders headers, BStruct wsConnection) {
    String[] subProtocols = wsService.getNegotiableSubProtocols();
    WebSocketInitMessage initMessage = (WebSocketInitMessage) wsConnection.getNativeData(WebSocketConstants.WEBSOCKET_MESSAGE);
    int idleTimeoutInSeconds = wsService.getIdleTimeoutInSeconds();
    HandshakeFuture future = initMessage.handshake(subProtocols, true, idleTimeoutInSeconds * 1000, headers);
    future.setHandshakeListener(new HandshakeListener() {

        @Override
        public void onSuccess(Session session) {
            BStruct serviceEndpoint = (BStruct) wsConnection.getNativeData(WebSocketConstants.WEBSOCKET_ENDPOINT);
            populateEndpoint(session, serviceEndpoint);
            wsConnection.addNativeData(WebSocketConstants.NATIVE_DATA_WEBSOCKET_SESSION, session);
            WebSocketOpenConnectionInfo connectionInfo = new WebSocketOpenConnectionInfo(wsService, serviceEndpoint);
            WebSocketConnectionManager.getInstance().addConnection(session.getId(), connectionInfo);
            Resource onOpenResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_OPEN);
            if (onOpenResource == null) {
                return;
            }
            List<ParamDetail> paramDetails = onOpenResource.getParamDetails();
            BValue[] bValues = new BValue[paramDetails.size()];
            bValues[0] = serviceEndpoint;
            // TODO handle BallerinaConnectorException
            Executor.submit(onOpenResource, new WebSocketEmptyCallableUnitCallback(), null, null, bValues);
        }

        @Override
        public void onError(Throwable throwable) {
            ErrorHandlerUtils.printError(throwable);
        }
    });
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HandshakeFuture(org.wso2.transport.http.netty.contract.websocket.HandshakeFuture) HandshakeListener(org.wso2.transport.http.netty.contract.websocket.HandshakeListener) Resource(org.ballerinalang.connector.api.Resource) WebSocketInitMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketInitMessage) List(java.util.List) Session(javax.websocket.Session)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)2 HandshakeFuture (org.wso2.transport.http.netty.contract.websocket.HandshakeFuture)2 List (java.util.List)1 Session (javax.websocket.Session)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Resource (org.ballerinalang.connector.api.Resource)1 Struct (org.ballerinalang.connector.api.Struct)1 WebSocketClientConnectorListener (org.ballerinalang.net.http.WebSocketClientConnectorListener)1 WebSocketService (org.ballerinalang.net.http.WebSocketService)1 HttpWsConnectorFactory (org.wso2.transport.http.netty.contract.HttpWsConnectorFactory)1 HandshakeListener (org.wso2.transport.http.netty.contract.websocket.HandshakeListener)1 WebSocketClientConnector (org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector)1 WebSocketInitMessage (org.wso2.transport.http.netty.contract.websocket.WebSocketInitMessage)1 WsClientConnectorConfig (org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig)1