package com.google.gwt.sample.simplechat.client;
import com.google.gwt.sockets.client.SocketException;
import com.google.gwt.sockets.client.SocketsFactory;
import com.google.gwt.sockets.client.TextSocket;
import com.google.gwt.sockets.client.impl.FlashTextSocketImpl;
import com.google.gwt.sockets.client.impl.JavaTextSocketImpl;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
/**
* Creates sockets implementation depending on user's choice.
*
* @author Aleksey Lagoshin
*/
class SocketsSwicth implements ClickListener {
private ChatClient client = ChatClient.getInstance();
public void onClick(Widget sender) {
// Closing active connection
if (client.getSocket().isConnected())
client.disconnect();
try {
// Shutting down the sockets object (this operation removes sockets object from the web page)
client.getSocket().shutdown();
}
catch (SocketException e) {
client.println(" ** Exception while shutting down the socket object: " + e.getMessage());
}
TextSocket socket = null;
String host = client.getHost();
int port = client.getPort();
String name = ((RadioButton) sender).getText();
if (name.equals("Flash")) {
// Flash 6 and later supports text sockets
if (SocketsFactory.isFlashXSupported(6))
socket = new FlashTextSocketImpl(new SocketHandler(), "xmlsocket://" + host + ":" + port);
else
client.println(" ** Flash is not supported by your browser");
}
else if (name.equals("Java")) {
if (SocketsFactory.isJavaSupported())
socket = new JavaTextSocketImpl(new SocketHandler());
else
client.println(" ** Java is not supported by your browser");
}
else
socket = SocketsFactory.createTextSocket(new SocketHandler(), "xmlsocket://" + host + ":" + port);
if (socket == null)
client.println(" ** Sockets module is not supported by your browser");
else
client.setSocket(socket);
}
}