0

I was creating a application using socket.io in native android application to communicate with websockets Tester. I have tested it using a online WebSocketServer , but each time when trying to check the connection in android device, 'Connection Error` is Logged.

Error : io.socket.engineio.client.EngineIOException: websocket error java.net.ProtocolException: Expected HTTP 101 response but was '404 Not Found'

onlineTestingPortal link :https://www.piesocket.com/websocket-tester

SocketServerCode:

package com.example.appsocket;


import android.util.Log;

import org.json.JSONObject;

import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import io.socket.engineio.client.transports.WebSocket;

public class SocketServer {

    private Socket mSocket;

    private static SocketServer lbInstance = new SocketServer();

    public static SocketServer getInstance() {
        return lbInstance;
    }


    public  void callSocketServer(){

        try {
            IO.Options options = new IO.Options();
            options.transports = new String[]{ WebSocket.NAME};

            mSocket = IO.socket("wss://demo.piesocket.com/v3/channel_123?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&notify_self",options);

            mSocket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    System.out.println("Connection Error");
                }
            });
            mSocket.connect();

            mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {

                    System.out.println("SocketServer: connected call back" + args[0].toString());
                    mSocket.emit("YOUR_TOPIC", "YOUR_DATA_VALUE");


                }
            });
        } catch (URISyntaxException e) {
            System.out.println("Exception is : " + e);
        }

    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       SocketServer.getInstance().callSocketServer();
    }
}
I'm Coder
  • 125
  • 2
  • 13

1 Answers1

0

Your problem is because of using socket.io library, you don't need socket.io.

In this link, the correct way to connect to ws protocol is mentioned

Ali Azmoodeh
  • 152
  • 2
  • 10