use of io.crossbar.autobahn.websocket.types.ConnectionResponse in project autobahn-java by crossbario.
the class WebSocketConnection method createHandler.
/**
* Create master message handler.
*/
private void createHandler() {
mMasterHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
// anything received after that.
if (onCloseCalled) {
LOGGER.d("onClose called already, ignore message.");
return;
}
if (msg.obj instanceof TextMessage) {
TextMessage textMessage = (TextMessage) msg.obj;
if (mWsHandler != null) {
mWsHandler.onMessage(textMessage.mPayload);
} else {
LOGGER.d("could not call onTextMessage() .. handler already NULL");
}
} else if (msg.obj instanceof RawTextMessage) {
RawTextMessage rawTextMessage = (RawTextMessage) msg.obj;
if (mWsHandler != null) {
mWsHandler.onMessage(rawTextMessage.mPayload, false);
} else {
LOGGER.d("could not call onRawTextMessage() .. handler already NULL");
}
} else if (msg.obj instanceof BinaryMessage) {
BinaryMessage binaryMessage = (BinaryMessage) msg.obj;
if (mWsHandler != null) {
mWsHandler.onMessage(binaryMessage.mPayload, true);
} else {
LOGGER.d("could not call onBinaryMessage() .. handler already NULL");
}
} else if (msg.obj instanceof Ping) {
Ping ping = (Ping) msg.obj;
LOGGER.d("WebSockets Ping received");
if (ping.mPayload == null) {
mWsHandler.onPing();
} else {
mWsHandler.onPing(ping.mPayload);
}
} else if (msg.obj instanceof Pong) {
Pong pong = (Pong) msg.obj;
if (pong.mPayload == null) {
mWsHandler.onPong();
} else {
mWsHandler.onPong(pong.mPayload);
}
LOGGER.d("WebSockets Pong received");
} else if (msg.obj instanceof Close) {
Close close = (Close) msg.obj;
final int crossbarCloseCode = (close.mCode == 1000) ? IWebSocketConnectionHandler.CLOSE_NORMAL : IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST;
if (close.mIsReply) {
LOGGER.d("WebSockets Close received (" + close.mCode + " - " + close.mReason + ")");
closeAndCleanup();
onClose(crossbarCloseCode, close.mReason);
} else if (mActive) {
// We have received a close frame, lets clean.
closeReaderThread(false);
mWriter.forward(new Close(1000, true));
mActive = false;
} else {
LOGGER.d("WebSockets Close received (" + close.mCode + " - " + close.mReason + ")");
// we've initiated disconnect, so ready to close the channel
closeAndCleanup();
onClose(crossbarCloseCode, close.mReason);
}
} else if (msg.obj instanceof ServerHandshake) {
ServerHandshake serverHandshake = (ServerHandshake) msg.obj;
LOGGER.d("opening handshake received");
if (serverHandshake.mSuccess) {
if (mWsHandler != null) {
mExecutor.scheduleAtFixedRate(mAutoPinger, 0, mOptions.getAutoPingInterval(), TimeUnit.SECONDS);
String protocol = getOrDefault(serverHandshake.headers, "Sec-WebSocket-Protocol", null);
mWsHandler.setConnection(WebSocketConnection.this);
mWsHandler.onConnect(new ConnectionResponse(protocol));
mWsHandler.onOpen();
LOGGER.d("onOpen() called, ready to rock.");
} else {
LOGGER.d("could not call onOpen() .. handler already NULL");
}
}
} else if (msg.obj instanceof CannotConnect) {
CannotConnect cannotConnect = (CannotConnect) msg.obj;
failConnection(IWebSocketConnectionHandler.CLOSE_CANNOT_CONNECT, cannotConnect.reason);
} else if (msg.obj instanceof ConnectionLost) {
ConnectionLost connnectionLost = (ConnectionLost) msg.obj;
failConnection(IWebSocketConnectionHandler.CLOSE_CONNECTION_LOST, connnectionLost.reason);
} else if (msg.obj instanceof ProtocolViolation) {
@SuppressWarnings("unused") ProtocolViolation protocolViolation = (ProtocolViolation) msg.obj;
failConnection(IWebSocketConnectionHandler.CLOSE_PROTOCOL_ERROR, "WebSockets protocol violation");
} else if (msg.obj instanceof Error) {
Error error = (Error) msg.obj;
failConnection(IWebSocketConnectionHandler.CLOSE_INTERNAL_ERROR, "WebSockets internal error (" + error.mException.toString() + ")");
} else if (msg.obj instanceof ServerError) {
ServerError error = (ServerError) msg.obj;
failConnection(IWebSocketConnectionHandler.CLOSE_SERVER_ERROR, "Server error " + error.mStatusCode + " (" + error.mStatusMessage + ")");
} else {
processAppMessage(msg.obj);
}
}
};
}
use of io.crossbar.autobahn.websocket.types.ConnectionResponse 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);
}
Aggregations