Search in sources :

Example 1 with HandshakeListener

use of org.wso2.transport.http.netty.contract.websocket.HandshakeListener 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

List (java.util.List)1 Session (javax.websocket.Session)1 Resource (org.ballerinalang.connector.api.Resource)1 BStruct (org.ballerinalang.model.values.BStruct)1 HandshakeFuture (org.wso2.transport.http.netty.contract.websocket.HandshakeFuture)1 HandshakeListener (org.wso2.transport.http.netty.contract.websocket.HandshakeListener)1 WebSocketInitMessage (org.wso2.transport.http.netty.contract.websocket.WebSocketInitMessage)1