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);
}
});
}
Aggregations