This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MoreliaTalk/develop
Develop
- Loading branch information
Showing
25 changed files
with
178 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
package ru.wtw.moreliatalkclient; | ||
|
||
import android.app.Activity; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.util.Log; | ||
import android.widget.ScrollView; | ||
import android.widget.TextView; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.NonReadableChannelException; | ||
|
||
import org.java_websocket.client.WebSocketClient; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
|
||
import com.google.gson.Gson; | ||
|
||
|
||
public class Network { | ||
private URI socketURI; | ||
private WebSocketClient socket; | ||
|
@@ -24,6 +26,10 @@ public class Network { | |
private String username; | ||
private String password; | ||
private String servername; | ||
private boolean reconnect; | ||
private boolean showJSON; | ||
private boolean useNewAPI; | ||
|
||
private boolean isConnected; | ||
|
||
public Network(Activity activity){ | ||
|
@@ -43,65 +49,97 @@ public void setPassword(String password) { | |
this.password = password; | ||
} | ||
|
||
public void setReconnect(boolean reconnect) { this.reconnect = reconnect; } | ||
|
||
public void setShowJSON(boolean showJSON) { this.showJSON = showJSON; } | ||
|
||
public void setUseNewAPI(boolean useNewAPI) { this.useNewAPI = useNewAPI; } | ||
|
||
public boolean isConnected() { | ||
return isConnected; | ||
} | ||
|
||
public void reconnect(){ | ||
final Handler handler = new Handler(Looper.getMainLooper()); | ||
handler.postDelayed(new Runnable() { | ||
@Override | ||
public void run() { | ||
socket.reconnect(); | ||
} | ||
}, 1000); | ||
} | ||
|
||
public void connect() { | ||
try { | ||
socketURI = new URI(servername); | ||
} catch (URISyntaxException e) { | ||
e.printStackTrace(); | ||
return; | ||
if (socketURI == null) { | ||
try { | ||
socketURI = new URI(servername); | ||
} catch (URISyntaxException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
} | ||
socket = new WebSocketClient(socketURI) { | ||
@Override | ||
if (socket == null) socket = new WebSocketClient(socketURI) { | ||
|
||
@Override | ||
public void onOpen(ServerHandshake handshakedata) { | ||
isConnected=true; | ||
sendAuth(); | ||
Log.i("SERVER","Connected"); | ||
isConnected = true; | ||
if (useNewAPI) { | ||
sendReg(); | ||
} else { | ||
sendAuth(); | ||
} | ||
Log.i("SERVER", "Connected"); | ||
} | ||
|
||
@Override | ||
public void onMessage(String message) { | ||
Protocol protocol = new Gson().fromJson(message,Protocol.class); | ||
String status=protocol.getStatus(); | ||
String reply; | ||
if (protocol.getMode().equals("message")) { | ||
reply = protocol.getUsername() + ": " + protocol.getText(); | ||
outChat(reply); | ||
} | ||
if (protocol.getMode().equals("reg")) { | ||
reply = activity.getResources().getString(R.string.auth_status_unknown); | ||
if (status.equals("true")) | ||
reply = activity.getResources().getString(R.string.auth_status_true); | ||
if (status.equals("false")) | ||
reply = activity.getResources().getString(R.string.auth_status_false); | ||
if (status.equals("newreg")) | ||
reply = activity.getResources().getString(R.string.auth_status_newreg); | ||
outChat(reply); | ||
if (showJSON) { | ||
outChat("Received: "+message); | ||
} else { | ||
Protocol protocol = new Gson().fromJson(message, Protocol.class); | ||
String status = protocol.getStatus(); | ||
String reply; | ||
if (protocol.getMode().equals("message")) { | ||
reply = protocol.getTime() + " - " + protocol.getUsername() + ": " + protocol.getText(); | ||
outChat(reply); | ||
} | ||
if (protocol.getMode().equals("reg")) { | ||
reply = activity.getResources().getString(R.string.auth_status_unknown); | ||
if (status.equals("true")) | ||
reply = activity.getResources().getString(R.string.auth_status_true); | ||
if (status.equals("false")) | ||
reply = activity.getResources().getString(R.string.auth_status_false); | ||
if (status.equals("newreg")) | ||
reply = activity.getResources().getString(R.string.auth_status_newreg); | ||
outChat(reply); | ||
} | ||
} | ||
Log.i("SERVER","Message: "+message); | ||
Log.i("SERVER", "Message: " + message); | ||
} | ||
|
||
@Override | ||
public void onMessage(ByteBuffer message) { | ||
Log.i("SERVER","Message byte buffer"); | ||
Log.i("SERVER", "Message byte buffer"); | ||
} | ||
|
||
@Override | ||
public void onClose(final int code, String reason, boolean remote) { | ||
Log.i("SERVER","Disconnected with exit code " + code + " additional info: " + reason); | ||
outChat(activity.getResources().getString(R.string.socket_close)+code); | ||
Log.i("SERVER", "Disconnected with exit code " + String.valueOf(code) + " additional info: " + reason); | ||
outChat(activity.getResources().getString(R.string.socket_close) + String.valueOf(code)); | ||
if (reconnect) { | ||
outChat(activity.getResources().getString(R.string.reconnecting)); | ||
Network.this.reconnect(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Exception ex) { | ||
Log.e("SERVER","Error", ex); | ||
outChat(activity.getResources().getString(R.string.socket_error)+ex.toString()); | ||
Log.e("SERVER", "Error", ex); | ||
outChat(activity.getResources().getString(R.string.socket_error) + ex.toString()); | ||
} | ||
}; | ||
|
||
Log.i("SERVER","Connect"); | ||
socket.connect(); | ||
|
||
|
@@ -125,6 +163,22 @@ public void run() | |
}); | ||
} | ||
|
||
public void sendReg () { | ||
Gson gson = new Gson(); | ||
Protocol protocol = new Protocol(); | ||
protocol.setMode("reg"); | ||
protocol.setUsername(username); | ||
protocol.setPassword(password); | ||
String json = "{ \"type\": \"register_user\", \"data\": { \"user\": { \"password\": \"" + password+ | ||
"\", \"login\": \""+username+"\", \"email\": \"[email protected]\", \"username\": \"User1\" }, "+ | ||
"\"meta\": \"None\" }, \"jsonapi\": { \"version\": \"1.0\" }, \"meta\": \"None\" }"; | ||
if (socket != null && socket.isOpen()) { | ||
if (showJSON) outChat("Sending: "+json); | ||
Log.i("SERVER","Send reg"); | ||
socket.send(json); | ||
} | ||
} | ||
|
||
public void sendAuth () { | ||
Gson gson = new Gson(); | ||
Protocol protocol = new Protocol(); | ||
|
@@ -133,6 +187,7 @@ public void sendAuth () { | |
protocol.setPassword(password); | ||
String json = gson.toJson(protocol); | ||
if (socket != null && socket.isOpen()) { | ||
if (showJSON) outChat("Sending: "+json); | ||
Log.i("SERVER","Send auth"); | ||
socket.send(json); | ||
} | ||
|
@@ -146,6 +201,7 @@ public void sendMessage (String text) { | |
protocol.setText(text); | ||
String json = gson.toJson(protocol); | ||
if (socket != null && socket.isOpen()) { | ||
if (showJSON) outChat("Sending: "+json); | ||
Log.i("SERVER","Send text"); | ||
Log.i("SERVER",json); | ||
socket.send(json); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.