Connection was rejected when using WebSocet to connect to the server

I am trying to connect to GlassFish-server4.1.1 from an Android application but giving me the Error failed to connect to / localhost (port 8080) after 90,000 ms I change the port on the server but give me the same error ......

it only connects to the server on netbeans, but on android give me faild and below my code

server code

 @ServerEndpoint("/echo") 

public class WebSocketClass {

 /**
 * @param session
 * @OnOpen allows us to intercept the creation of a new session.
 * The session class allows us to send data to the user.
 * In the method onOpen, we'll let the user know that the handshake was 
 * successful.
 */


@OnOpen
public void onOpen(Session session){
    System.out.println(session.getId() + " has opened a connection"); 
    try {
        session.getBasicRemote().sendText("Connection Established");
    } catch (IOException ex) {
    }
}

/**
 * When a user sends a message to the server, this method will intercept the message
 * and allow us to react to it. For now the message is read as a String.
 * @param message
 * @param session
 */
@OnMessage
public void onMessage12(String message, Session session){
    System.out.println("Message from " + session.getId() + ": " + message);
    try {
        session.getBasicRemote().sendText(message);
    } catch (IOException ex) {
    }
}

/**
 * The user closes the connection.
 * 
 * Note: you can't send messages to the client from this method
 * @param session
 */
@OnClose
public void onClose(Session session){
    System.out.println("Session " +session.getId()+" has ended");
}}

Client code

private void connectWebSocket() {
    URI uri;
    try {
        uri = new URI("ws://localhost:8080/WebApplication1/echo");
    } catch (URISyntaxException e) {
        e.printStackTrace();
        return;
    }

    mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.i("Websocket", "Opened");
            mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
        }

        @Override
        public void onMessage(String s) {
            final String message = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView textView = (TextView)findViewById(R.id.messages);
                    textView.setText(textView.getText() + "\n" + message);
                }
            });
        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.i("Websocket", "Closed " + s);
        }

        @Override
        public void onError(Exception e) {
            Log.i("Websocket", "Error " + e.getMessage());
        }
    };
    mWebSocketClient.connect();
}

I do not know why this error? can someone help me ...

+4
source share
1 answer

Perhaps you should change the IP address to reflect the IP address on which the Glassfish server is installed on the client.

0
source

Source: https://habr.com/ru/post/1691306/


All Articles