|
|
@@ -29,111 +29,51 @@ import javax.net.ssl.SSLContext;
|
|
|
/**
|
|
|
* Created by david on 16/04/16.
|
|
|
*/
|
|
|
-public class chatFragment extends Fragment {
|
|
|
- private static final String WSS_URL = "wss://ws.especificosba.com.ar/";
|
|
|
+public class chatFragment extends Fragment implements chatListener {
|
|
|
private TextView chat;
|
|
|
- private WebSocket ws;
|
|
|
- private enum WS_STATUS {
|
|
|
- CONNECTED,
|
|
|
- DISCONNECTED
|
|
|
- };
|
|
|
- private WS_STATUS status = WS_STATUS.DISCONNECTED;
|
|
|
+
|
|
|
@Override
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
Bundle savedInstanceState) {
|
|
|
ViewGroup rootView = (ViewGroup) inflater.inflate(
|
|
|
R.layout.fragment_chat , container, false);
|
|
|
-
|
|
|
-
|
|
|
return rootView;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
- connectWebSocket();
|
|
|
Button b = (Button) getView().findViewById(R.id.button);
|
|
|
- chat =(TextView) getView().findViewById(R.id.chat);
|
|
|
b.setOnClickListener(new Button.OnClickListener(){
|
|
|
@Override
|
|
|
public void onClick(View b){
|
|
|
sendMessage();
|
|
|
}
|
|
|
});
|
|
|
- chat.setMovementMethod(new ScrollingMovementMethod());
|
|
|
- }
|
|
|
-
|
|
|
- private void connectWebSocket() {
|
|
|
- try {
|
|
|
- WebSocketFactory factory = new WebSocketFactory();
|
|
|
- SSLContext context = NaiveSSLContext.getInstance("TLS");
|
|
|
-
|
|
|
- // Set the custom SSL context.
|
|
|
- factory.setSSLContext(context);
|
|
|
- ws = factory.createSocket(WSS_URL);
|
|
|
- } catch (Exception e) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- ws.addListener(new WebSocketAdapter() {
|
|
|
- @Override
|
|
|
- public void onTextMessage(WebSocket websocket, String message) throws Exception {
|
|
|
- Log.i("Websocket", "msg:" + message);
|
|
|
- JSONObject obj = new JSONObject(message);
|
|
|
- switch (obj.getString("type")) {
|
|
|
- case "chat":
|
|
|
-
|
|
|
- String formattedMessage = String.format("\n[HORA] %s: %s", obj.getString("user"), obj.getString("text"));
|
|
|
- chat.append(formattedMessage);
|
|
|
- final Layout layout = chat.getLayout();
|
|
|
- if(layout != null){
|
|
|
- int scrollDelta = layout.getLineBottom(chat.getLineCount() - 1)
|
|
|
- - chat.getScrollY() - chat.getHeight();
|
|
|
- Log.i("Websocket", Integer.toString(scrollDelta));
|
|
|
- if(scrollDelta > 0)
|
|
|
- chat.scrollBy(0, scrollDelta);
|
|
|
- }
|
|
|
- chat.invalidate();
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- ;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
|
|
- Log.i("Websocket", "Connected");
|
|
|
- status = WS_STATUS.CONNECTED;
|
|
|
- chat.append("\nConectado al sistema");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame,
|
|
|
- boolean closedByServer) {
|
|
|
- Log.i("Websocket", "Disconnected");
|
|
|
- status = WS_STATUS.DISCONNECTED;
|
|
|
- websocket.connectAsynchronously();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onConnectError(WebSocket websocket, WebSocketException cause) {
|
|
|
- Log.i("Websocket", "Didn't connect" + cause.getMessage());
|
|
|
- status = WS_STATUS.DISCONNECTED;
|
|
|
- }
|
|
|
- }).connectAsynchronously();
|
|
|
+ chat =(TextView) getView().findViewById(R.id.chat);
|
|
|
+ //chat.setMovementMethod(new ScrollingMovementMethod());
|
|
|
}
|
|
|
|
|
|
- public void sendMessage() {
|
|
|
+ public void sendMessage(){
|
|
|
EditText editText = (EditText) getView().findViewById( R.id.send_text );
|
|
|
String text=editText.getText().toString();
|
|
|
if(text.length() < 3) return;
|
|
|
-
|
|
|
- String formatted=String.format("{ \"type\":\"chat\", \"user\": \"%s %s (%s)\", \"text\":\"%s\" }", "nombre","Apellido","pais", text);
|
|
|
- Log.i("Websocket", "sending" + formatted);
|
|
|
-
|
|
|
- ws.sendText(formatted);
|
|
|
+ WebSocketHandler.sendMessage(text);
|
|
|
editText.setText("");
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void msgReceived(String text) {
|
|
|
+ Log.i("Websocket", "msg received: " + text);
|
|
|
+ //chat.append(result);
|
|
|
+ chat.setText(chat.getText() + "\ntext");
|
|
|
+ /*final Layout layout = chat.getLayout();
|
|
|
+ if(layout != null){
|
|
|
+ int scrollDelta = layout.getLineBottom(chat.getLineCount() - 1)
|
|
|
+ - chat.getScrollY() - chat.getHeight();
|
|
|
+ if(scrollDelta > 0)
|
|
|
+ chat.scrollBy(0, scrollDelta);
|
|
|
+ }*/
|
|
|
+ chat.invalidate();
|
|
|
+ }
|
|
|
}
|