WebSocket连接失败

时间:2015-03-20 10:45:26

标签: websocket

我在浏览器中遇到此问题请提出解决方案

  

与'wss://192.168.1.210:8080 /'的WebSocket连接失败:错误输入   连接建立:net :: ERR_TIMED_OUT

1 个答案:

答案 0 :(得分:0)

public class connectTask extends AsyncTask<String, String, TCPClient> {

        @Override
        protected TCPClient doInBackground(String... message) {

            // we create a TCPClient object and
            mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
                @Override
                // here the messageReceived method is implemented
                public void messageReceived(String message) {
                    // this method calls the onProgressUpdate
                    publishProgress(message);
                }
            });
            mTcpClient.run();
            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            //servermsg1 += values[0];
            //writedatabase(servermsg1);
            // in the arrayList we add the messaged received from server
             arrayList.add(values[0]);
            // notify the adapter that the data set has changed. This means that
            // new message received
            // from server was added to the list
             mAdapter.notifyDataSetChanged();
        }
    }

调用此方法调用asynctask

connectTask connect = new connectTask();
        connect.execute("");

创建课程

public class TCPClient {
    private String serverMessage;


    public static final String SERVERIP = "192.168.1.210"; //your computer IP address
    public static final int SERVERPORT = 8080;
    private OnMessageReceived mMessageListener = null;
    private boolean mRun = false;
    DataInputStream is;
    PrintWriter out;
    BufferedReader in;
    Socket socket;
    String root=Environment.getExternalStorageState();
    /**
     *  Constructor of the class. OnMessagedReceived listens for the messages received from server
     */
    public TCPClient(OnMessageReceived listener) {
        mMessageListener = listener;
        mRun = true;
    }
    /**
     * Sends the message entered by client to the server
     * @param message text entered by client
     */
    public void sendMessage(String message){
        if (out != null && !out.checkError()) {
            out.println(message);
            Log.i("msg to the server",""+message);
            out.flush();
            mRun = true;
        }
    }
    public void stopClient(){
        mRun = false;
    }
    public void run() {
       // mRun = true;
        try {
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);
            Log.e("TCP Client", "C: Connecting...");
            //create a socket to make the connection with the server
            socket = new Socket(serverAddr, SERVERPORT);
            socket.setKeepAlive(true);
            try {
                //send the message to the server
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                Log.i("TCP Client", "C: Sent.");
                Log.i("TCP Client", "C: Done.");
                 is = new DataInputStream(socket.getInputStream());
                 // get rid of the array length and convert to string
                  // close the connection
                //receive the message which the server sends back
               in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                //in this while the client listens for the messages sent by the server
                while (mRun) {
                      serverMessage = in.readLine();
                       //serverMessage = buffer.toString("UTF-16LE").substring(8);
                       if (serverMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                        mMessageListener.messageReceived(serverMessage);
                    }
                    serverMessage = null;
                }
                Log.e("RESPONSE FROM SERVER", "S: Received Message: " + serverMessage +"'");
            } catch (Exception e) {
                Log.e("TCP", "S: Error", e);
            } finally {
                //the socket must be closed. It is not possible to reconnect to this socket
                // after it is closed, which means a new socket instance has to be created.
                socket.close();
            }
        } catch (Exception e) {
            Log.e("TCP", "C: Error", e);
        }
    }
    //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
    //class at on asynckTask doInBackground
    public interface OnMessageReceived {

        public void messageReceived(String message);


    }

}
相关问题