Java ServerSocket WebSocket 回复

发布于 2024-11-24 05:51:49 字数 2857 浏览 1 评论 0原文

我正在尝试使用 Java 创建自己的 WebSocket 服务器。

当我的客户端连接时,我收到以下请求:(

(14): GET / HTTP/1.1
(18): Upgrade: WebSocket
(19): Connection: Upgrade
(20): Host: localhost:8483
(24): Origin: http://localhost
(45): Sec-WebSocket-Key1: P3$04 H85Zf# 9 9d a0 x10[
(34): Sec-WebSocket-Key2: 416393 2  560Y
(0): 

括号中的数字、方括号、冒号及其后的空格只是我为 System.out.println() 命令添加的内容)。括号中的数字是以字节为单位的行长度。

我首先使用此函数处理请求:

public boolean processHandshake(int lineNumber, String line){

    if(handshakeProcessed || lineNumber > 9 || lineNumber < 1){

        return false;

    }

    switch(lineNumber){

        case 1:{ handshakeGetLocation = line.replace("GET ", "").replace(" HTTP/1.1", ""); break; }
        case 2:{ handshakeUpgrade = line.replace("Upgrade: ", ""); break; }
        case 3:{ handshakeConnection = line.replace("Connection: ", ""); break; }
        case 4:{ handshakeHost = line.replace("Host: : ", ""); break; }
        case 5:{ handshakeOrigin = line.replace("Origin: ", ""); break; }
        case 6:{ handshakeSecWebSocketKey1 = line.replace("Sec-WebSocket-Key1: ", ""); break; }
        case 7:{ handshakeSecWebSocketKey2 = line.replace("Sec-WebSocket-Key2: ", ""); handshakeProcessed = false; break; }
        case 8:{ handshakeProcessed = true; }
        case 9:{ handshakeProcessed = true; }

    }

    return true;

}

现在,根据 这篇 文章并假设它是第一个版本对于我需要处理的协议,我一直想知道如何处理商:

问题是,对于每个密钥,我需要将位数除以空格数。我一直这样做:

private double calculateKeyReply(String key){

    double numCount = key.replaceAll("[^0-9]", "").length();
    double spaceCount = key.replaceAll("[^\\ ]", "").length();

    System.out.println(numCount+"/"+spaceCount+"="+numCount/spaceCount);

    return numCount/spaceCount;

}

并调用以下函数(replyHandshake()):

String handshake;

handshake = "HTTP/1.1 101 WebSocket Protocol Handshake\n";
handshake += "Upgrade: "+handshakeUpgrade+"\n"; // handshakeUpgrade and the following variables are instance variables I set when I process the request
handshake += "Connection: "+handshakeConnection+"\n";
handshake += "Sec-WebSocket-Origin: "+handshakeOrigin+"\n";
handshake += "Sec-WebSocket-Location: "+handshakeOrigin.replace("http", "ws")+handshakeGetLocation+"\n";
handshake += "Sec-WebSocket-Protocol: sample\n";
// handshake += "\n";

String nums = calculateKeyReply(handshakeSecWebSocketKey1)+""+calculateKeyReply(handshakeSecWebSocketKey2);

MessageDigest md5Digestor = MessageDigest.getInstance("MD5");
String md5 = new String(md5Digestor.digest(nums.getBytes()));

handshake += md5;

return handshake;

然后,在其他地方:

out.println(replyHandshake());

我做错了什么吗?我正在使用最新版本的 Google Chrome 对其进行测试。

提前致谢!

I am trying to create my own WebSocket Server with Java.

When my client connects, I get following request:

(14): GET / HTTP/1.1
(18): Upgrade: WebSocket
(19): Connection: Upgrade
(20): Host: localhost:8483
(24): Origin: http://localhost
(45): Sec-WebSocket-Key1: P3$04 H85Zf# 9 9d a0 x10[
(34): Sec-WebSocket-Key2: 416393 2  560Y
(0): 

(The numbers in brackets, the brackets, the colons and the spaces thereafter only being something I add for the System.out.println() command). The numbers in brackets are the length of the line in bytes.

I first process the request using this function:

public boolean processHandshake(int lineNumber, String line){

    if(handshakeProcessed || lineNumber > 9 || lineNumber < 1){

        return false;

    }

    switch(lineNumber){

        case 1:{ handshakeGetLocation = line.replace("GET ", "").replace(" HTTP/1.1", ""); break; }
        case 2:{ handshakeUpgrade = line.replace("Upgrade: ", ""); break; }
        case 3:{ handshakeConnection = line.replace("Connection: ", ""); break; }
        case 4:{ handshakeHost = line.replace("Host: : ", ""); break; }
        case 5:{ handshakeOrigin = line.replace("Origin: ", ""); break; }
        case 6:{ handshakeSecWebSocketKey1 = line.replace("Sec-WebSocket-Key1: ", ""); break; }
        case 7:{ handshakeSecWebSocketKey2 = line.replace("Sec-WebSocket-Key2: ", ""); handshakeProcessed = false; break; }
        case 8:{ handshakeProcessed = true; }
        case 9:{ handshakeProcessed = true; }

    }

    return true;

}

Now, according to this article and assuming it's the first version of the protocol I need to process, I've been wondering how to deal with the quotients:

The thing is, for each key, I need to divide the number of digits by that of the spaces. I've been doing it like that:

private double calculateKeyReply(String key){

    double numCount = key.replaceAll("[^0-9]", "").length();
    double spaceCount = key.replaceAll("[^\\ ]", "").length();

    System.out.println(numCount+"/"+spaceCount+"="+numCount/spaceCount);

    return numCount/spaceCount;

}

And calling following function (replyHandshake()):

String handshake;

handshake = "HTTP/1.1 101 WebSocket Protocol Handshake\n";
handshake += "Upgrade: "+handshakeUpgrade+"\n"; // handshakeUpgrade and the following variables are instance variables I set when I process the request
handshake += "Connection: "+handshakeConnection+"\n";
handshake += "Sec-WebSocket-Origin: "+handshakeOrigin+"\n";
handshake += "Sec-WebSocket-Location: "+handshakeOrigin.replace("http", "ws")+handshakeGetLocation+"\n";
handshake += "Sec-WebSocket-Protocol: sample\n";
// handshake += "\n";

String nums = calculateKeyReply(handshakeSecWebSocketKey1)+""+calculateKeyReply(handshakeSecWebSocketKey2);

MessageDigest md5Digestor = MessageDigest.getInstance("MD5");
String md5 = new String(md5Digestor.digest(nums.getBytes()));

handshake += md5;

return handshake;

And then, somewhere else:

out.println(replyHandshake());

Am I doing something wrong? I'm testing it with the latest version of Google Chrome.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-12-01 05:51:49

如果您今天加倍努力并从头开始为自己实现一个服务器,我将瞄准该协议的最新版本(版本 8,草案 10)。

上面的握手来自过时的版本。

Chrome 14 和 Firefox 7/8 支持最新版本。 Firefox 6 有一个(默认禁用)旧版本。 Chrome 很可能会放弃对任何 <8 版本的支持。

If you go the extra mile and implement a server for yourself from scratch today, I would target the latest version of the protocol (version 8, draft 10).

The above handshake is from an outdated version.

Chrome 14 and Firefox 7/8 support the latest. Firefox 6 has a (disabled by default) old version. Chrome might very well drop support for any version <8.

安人多梦 2024-12-01 05:51:49

您还可以使用名为 MINA 的 Apache 库,它有一个 创建 Web 套接字

You can also use the Apache libary called MINA, which has a library for creating web sockets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文