Skip to content
New issue

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

Please help, socket connection not working always fail #118

Open
Joshuaap458 opened this issue Mar 17, 2021 · 0 comments
Open

Please help, socket connection not working always fail #118

Joshuaap458 opened this issue Mar 17, 2021 · 0 comments

Comments

@Joshuaap458
Copy link

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();
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant