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

JavaScript客户端和Android服务器之间的套接字连接,javascript,android,sockets,websocket,socket.io,Javascript,Android,Sockets,Websocket,Socket.io,我有一个安卓服务器插座,需要在本地连接一个“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

我有一个安卓服务器插座,需要在本地连接一个“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;
    }
}
package com.artificio.remotesetup;
导入android.content.Context;
导入android.net.wifi.ScanResult;
导入android.util.Log;
导入com.artificioo.util.Utils;
导入java.io.IOException;
导入java.net.InetSocketAddress;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.ArrayList;
/**
*由Artificio于2016年5月11日创建。
*/
公共类RemoteSetupServer扩展线程{
私人最终语境;
私有ArrayList scanResults=null;
私有ArrayList个人服务器;
公共RemoteSetupServer(上下文、ArrayList扫描结果){
this.context=上下文;
this.scanResults=scanResults;
}
公开募捐{
试一试{
showToastInformationFastOnlyBeta(上下文,“运行RemoteSetupServer”);
ServerSocket ServerSocket=新的ServerSocket();
serverSocket.setReuseAddress(true);
bind(新的InetSocketAddress(CommunicationProtocol.SERVER_PORT));
individualServers=new ArrayList();
Utils.showtostinformationFastOnlyBeta(上下文,“服务器在线”);
布尔监听=真;
边听{
试一试{
Socket clientSocket=serverSocket.accept();
setTcpNoDelay(true);
RemoteSetupIndividualServer individualServer=
新的RemoteSetupIndividualServer(clientSocket、scanResults、this);
individualServer.start();
对于(int i=0;i
我尝试用两种不同的方式使用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>

无功插座;
套接字=新的WebSocket('ws://192.168.43.1:44345');
socket.onopen=function(){
警报(“打开”);
};
socket.onmessage=函数{
//警报('收到回复'+s);
};  
socket.onerror=函数(evt){console.log(evt);};
答复1:

html:8到“ws://192.168.43.1:44345/”的WebSocket连接 失败:WebSocket握手期间出错: net::错误\u无效\u HTTP\u响应


方式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>

var socket=io.connect('http://192.168.43.1:44345');
socket.on('connect',function(){
警报(“打开”);
});
on('event',function(data){});
on('disconnect',function(){});
答复2:

轮询xhr.js:261get net::错误\u无效\u HTTP\u响应



有什么想法吗?谢谢。

当年我制作游戏服务器时(用java,但类仍然存在),你可以使用Socket和ServerSocket连接到android设备。如果出于某种原因需要JS,那么忽略此注释服务器代码是什么样子的?这是一个websocket服务器?我刚刚添加了Android服务器套接字代码@SamiKuhmonenNo,服务器必须使用正确的协议。您是否理解
websocket
不是正常的TCP套接字。它有一个HTTP连接启动,然后“升级”到webSocket协议,然后使用特定的webSocket安全和数据帧格式。要处理来自浏览器的传入webSocket连接,您必须有一个使用webSocket协议的服务器。您可以在此处查看有关其工作原理的更多信息:。