We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public boolean isConnected; private Socket socket; { try{ socket = IO.socket("https://080f0676fdfb.ngrok.io/"); }catch(URISyntaxException e){ throw new RuntimeException(e); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_socket2); ButterKnife.bind(this);
// setHasOptionsMenu(true); socket.connect(); socket.on("message", handleIncomingMessages); socket.on(Socket.EVENT_CONNECT, onConnect); socket.on(Socket.EVENT_DISCONNECT, onDisconnect); socket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); socket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); socket.on(Socket.EVENT_ERROR, onError); }
@Override public void onDestroy() { super.onDestroy(); socket.disconnect(); socket.off(Socket.EVENT_CONNECT, onConnect); socket.off(Socket.EVENT_DISCONNECT, onDisconnect); socket.off(Socket.EVENT_CONNECT_ERROR, onConnectError); socket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); socket.off(Socket.EVENT_ERROR, onError); } public void initLayout(){ button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String message = etSocket.getText().toString(); etSocket.setText(" "); socket.emit("message", message); } }); } private Emitter.Listener handleIncomingMessages = new Emitter.Listener(){ @Override public void call(final Object... args){ runOnUiThread(new Runnable() { @Override public void run() { JSONObject data = (JSONObject) args[0]; String message; try { message = data.getString("message").toString(); } catch (JSONException e) { return; } Log.i("asd", message); } }); } }; private Emitter.Listener onConnect = new Emitter.Listener() { @Override public void call(Object... args) { isConnected = true; runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), "Connected...", Toast.LENGTH_LONG) .show(); isConnected = true; } }); } }; private Emitter.Listener onDisconnect = new Emitter.Listener() { @Override public void call(Object... args) { isConnected = false; runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), "Disconnected....", Toast.LENGTH_LONG) .show(); isConnected = false; } }); } }; private Emitter.Listener onConnectError = new Emitter.Listener() { @Override public void call(Object... args) { isConnected = false; runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), "Failed to connect...", Toast.LENGTH_LONG) .show(); isConnected = false; } }); } }; private Emitter.Listener onError = new Emitter.Listener() { @Override public void call(Object... args) { Toast.makeText(getBaseContext(), "onError", Toast.LENGTH_LONG) .show(); } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
public boolean isConnected;
private Socket socket;
{
try{
socket = IO.socket("https://080f0676fdfb.ngrok.io/");
}catch(URISyntaxException e){
throw new RuntimeException(e);
}
}
// setHasOptionsMenu(true);
socket.connect();
socket.on("message", handleIncomingMessages);
socket.on(Socket.EVENT_CONNECT, onConnect);
socket.on(Socket.EVENT_DISCONNECT, onDisconnect);
socket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
socket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
socket.on(Socket.EVENT_ERROR, onError);
}
The text was updated successfully, but these errors were encountered: