chrome(客户端)和热点(服务器)之间的Websocket通信(状态行未以CRLF结束)



这就是我所拥有的:

  • 我为我的手机创建了android应用程序,我正在使用谷歌播放的应用程序作为服务器
  • 我打开我的热点,用户可以使用ip和端口访问我的wifi,例如:192.168.xxx.xxx:8080

他会看到我的网站。在那里,我使用websockets在javascript和android java之间传递数据。

在Firefox和浏览器中,它运行良好,但在Chrome中,它告诉我:"到"ws://192.168.xxx.xxx:9999/"的Websocket连接失败:Websocket握手期间出错:状态行未以CRLF结尾;。

我使用了这里的代码:编写WebSocket服务器(参见下面的文件"编辑")。

我还阅读了RFC 6455:https://www.rfc-editor.org/rfc/rfc6455我完全按照上面写的那样做。

ClientSession的此示例:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientSession {
    private Socket socket;
    public ClientSession(Socket socket) {
        System.out.println("new ClientSessionTest()");
        this.socket = socket;
        initClientListener();
    }
    private void initClientListener() {
        System.out.println("initClientListener()");
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedReader socketReader = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                    final PrintWriter socketWriter = new PrintWriter(socket
                            .getOutputStream(), true);
                    String clientKey = null;
                    String responseKey = null;
                    while (true) {
                        String line = socketReader.readLine();
                        if (line == null) {
                            System.out.println("received null from client - closing connection");
                            break;
                        } else if (line.isEmpty()) {
                            System.out.println("empty line");
                            String _01 = "HTTP/1.1 101 Switching Protocols";
                            String _02 = "Upgrade: websocket";
                            String _03 = "Connection: Upgrade";
                            String _04 = "Sec-WebSocket-Accept: " + responseKey;
                            String _05 = "Sec-WebSocket-Protocol: chat";
                            String _06 = "Content-Encoding: identity";
                            System.out.println(_01);
                            System.out.println(_02);
                            System.out.println(_03);
                            System.out.println(_04);
                            System.out.println(_05);
                            System.out.println(_06);
                            System.out.println("");
                            socketWriter.println(_01);
                            socketWriter.println(_02);
                            socketWriter.println(_03);
                            socketWriter.println(_04);
                            socketWriter.println(_05);
                            socketWriter.println(_06);
                            socketWriter.println("");
                            //********************data from client*********************
                    
                            try {
                                byte[] buff = new byte[100];
                                int length = socket.getInputStream().read(buff);
                                byte[] bstr = new byte[length];
                                System.arraycopy(buff, 0, bstr, 0, length);
                                System.out.println(new String(bstr));
                                for (byte b : bstr) {
                                    System.out.print(((int) b) + " ");
                                }
                                System.out.println();
                                System.out.println();
                                String str = new String(decodeFrame(buff),"UTF-8");
                                System.out.println(str);
                            } catch (Exception e) {
                                System.out.println(e.getMessage());
                            
                            //********************************************************
                            
                        }
                        } else if (line.startsWith("Sec-WebSocket-Key:")) {
                            clientKey = line.replace("Sec-WebSocket-Key: ", "");
                            responseKey = ResponseGenerator
                                    .toResponseKey(clientKey);
                        } else {
                            System.out.println("" + line);
                            //socketWriter.println("lala");
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

我更改了行字符串_01="HTTP/1.1101交换协议"字符串_01="HTTP/1.1 101交换协议\r\n"及其删除我上面写的错误(CRLF),但javascript代码中的onopen方法(下面)没有启动,之后Firefox和Explorer也无法工作。

Javascript:

<html>
<head>
 
<script type="text/javascript" >
 
var websocket;
var url = "ws://localhost:1234";
 
function init(){
        try{
                websocket = new MozWebSocket(url, "chat");
        }catch(e){
                websocket = new WebSocket(url, "char");
        }
       
        websocket.onopen = function(evt) { onOpen(evt) };
        websocket.onclose = function(evt) { onClose(evt) };
        websocket.onmessage = function(evt) { onMessage(evt) };
        websocket.onerror = function(evt) { onError(evt) };
}
 
var count = 0;
function loop(){
        var message = "lalan";
        websocket.send(message);
        count++;
       
        setTimeout(loop, 500);
}
 
function onOpen(event){  
    alert("Socket has been opened!" + ('5' + 3) + ('5' - 3));  
 
    loop();
}  
 
function onMessage(evt){  
    alert(evt);  
}  
 
function onClose(event){  
    alert("socket closed");  
}
 
function onError(event){
        alert(event.data);
}
 
window.addEventListener("load", init, false);
 
 
</script>
 
</head>
<body>
 
 
 
</body>
</html>

注意:

我的手机没有网络连接(没有wifi或3g)。连接仅从用户到我的访问点。

我认为这个错误有点误导。每个标题都应该以CRLF结尾,而不仅仅是第一个("状态行")。至少根据测试用例。

String _01 = "HTTP/1.1 101 Switching Protocolsr"; // Added r here.
String _02 = "Upgrade: websocketr"; // Added r here.
String _03 = "Connection: Upgrader"; // Added r here.
String _04 = "Sec-WebSocket-Accept: " + responseKey + "r"; // Added r here.
String _05 = "Sec-WebSocket-Protocol: chatr"; // Added r here.
String _06 = "Content-Encoding: identityr"; // Added r here.

另外,在最后一个socketWriter.println("")-中添加另一个\r

socketWriter.println("r"); // Added r here.

也许这将标志着回应的结束。

此外,您可以使用Fiddler2(或Wireshark,但它太冗长了)来比较WebSocket实现(在网络数据方面)和有效实现之间的差异。

相关内容

  • 没有找到相关文章

最新更新