JavaScript客户端和Android服务器之间的套接字连接

时间:2017-04-11 15:29:12

标签: javascript android sockets websocket socket.io

我有一个Android服务器套接字,需要连接一个" web客户端"本地。

这是我的Android服务器套接字:

package com.artificioo.remotesetup;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.util.Log;

import com.artificioo.util.Utils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Created by artificioo on 11/05/16.
 */
public class RemoteSetupServer extends Thread {

    private final Context context;
    private ArrayList<ScanResult> scanResults = null;
    private ArrayList<RemoteSetupIndividualServer> individualServers;

    public RemoteSetupServer(Context context, ArrayList<ScanResult> scanResults) {
        this.context = context;
        this.scanResults = scanResults;
    }

    public void run() {
        try {
            Utils.showToastInformationFastOnlyBeta(context, "run RemoteSetupServer");
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(CommunicationProtocol.SERVER_PORT));

            individualServers = new ArrayList<>();
            Utils.showToastInformationFastOnlyBeta(context, "Server online");
            boolean listening = true;
            while (listening) {
                try {
                    Socket clientSocket = serverSocket.accept();
                    clientSocket.setTcpNoDelay(true);
                    RemoteSetupIndividualServer individualServer =
                            new RemoteSetupIndividualServer(clientSocket, scanResults, this);
                    individualServer.start();
                    for (int i = 0; i < individualServers.size(); i++) {
                        RemoteSetupIndividualServer remoteControlIndividualServer =
                                individualServers.get(i);
                        if (clientSocket.getInetAddress().getHostAddress().equals(
                                remoteControlIndividualServer.getClientSocket().getInetAddress()
                                        .getHostAddress())) {
                            Utils.showToastInformationFastOnlyBeta(context, "Client removed");
                            individualServers.remove(i);
                            break;
                        }
                    }
                    individualServers.add(individualServer);
                } catch (IOException | VerifyError e) {
//                    e.printStackTrace();
                }
            }
            Utils.showToastInformationFastOnlyBeta(context, "Server stop");
            Log.i(CommunicationProtocol.TAG, "Server stop");

            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            Utils.showToastInformationFastOnlyBeta(context, "Server stop (exception)");
            Log.i(CommunicationProtocol.TAG, "Server stop (exception)");
            e.printStackTrace();
        }


    }

    public int getClientCount() {
        return individualServers.size();
    }

    void disconnectMe(RemoteSetupIndividualServer remoteControlIndividualServer) {
        individualServers.remove(remoteControlIndividualServer);
    }

    public Context getContext() {
        return context;
    }
}

我尝试以两种不同的方式使用JavaScript客户端,但我遇到了问题。

方式1:

<html>
    <head>

        <script type="text/javascript">
        var socket;

        socket= new WebSocket('ws://192.168.43.1:44345');
        socket.onopen= function() {
            alert("is open");    
        };
        socket.onmessage= function(s) {
            //alert('got reply '+s);
        };  

        socket.onerror = function(evt) { console.log(evt); };
        </script>

    </head>

    <body>
    </body>
</html>

回应1:

  

testSocket.html:8 WebSocket连接到&#39; ws://192.168.43.1:44345 /&#39;   失败:WebSocket握手期间出错:   净:: ERR_INVALID_HTTP_RESPONSE

方式2:

<html>

    <head>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
        <script>
        var socket = io.connect('http://192.168.43.1:44345');
        socket.on('connect', function(){
            alert("is open");
        });
        socket.on('event', function(data){});
        socket.on('disconnect', function(){});
        </script> 

    </head>
    <body>
    </body>

</html>

回应2:

  

polling-xhr.js:261 GET   http://192.168.43.1:44345/socket.io/?EIO=3&transport=polling&t=LjTe5ai   净:: ERR_INVALID_HTTP_RESPONSE

有什么想法吗?感谢。

0 个答案:

没有答案