【问题标题】:Java ServerSocket WebSocket replyJava ServerSocket WebSocket 回复
【发布时间】:2011-07-15 09:55:14
【问题描述】:

我正在尝试使用 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;

}

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

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

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 对其进行测试。

提前致谢!

【问题讨论】:

  • 您是否真的附加了握手请求的最后一个字节(维基百科文章中的^n:ds[4U )?
  • 你是对的,我一直做错了。现在我实际上将我进入的前 2048 个字节存储到一个字节数组中(遗憾的是,它仍然不起作用,但是:()
  • 标题和键之间应该有两个换行符,并且所有换行符都必须是\r\n而不是\n
  • 啊,谢谢,可能也是个问题...

标签: java google-chrome protocols websocket serversocket


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您还可以使用名为 MINA 的 Apache 库,它有一个用于 creating web sockets 的库。

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      相关资源
      最近更新 更多