Vla*_*ian 2 java websocket handshake
所以,我正在尝试解析一个使用websocket向客户端发送数据的网站.从我读过的内容:
我知道,为了创建连接,我需要向服务器发送一个特殊的http请求,然后我将检索一些用于访问该通道的数据.
我做了什么:
创建了http请求
`URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Host", "www.example.com");
con.setRequestProperty("Upgrade", "websocket");
con.setRequestProperty("Connection", "upgrade");
con.setRequestProperty("Origin", "https://example.com");
con.setRequestProperty("Sec-WebSocket-Extensions", "x-webkit-deflate-frame");
con.setRequestProperty("Sec-WebSocket-Key", "QkWkChtTGpaZL5PkOMaRtg==");
con.setRequestProperty("Sec-WebSocket-Version", "13");
con.setRequestProperty("User-Agent", "Mozilla/..;
Run Code Online (Sandbox Code Playgroud)
`
我用代码200(不应该是101)读取响应.
问题:
1.1.还能做些什么?
1.2.来自http的相同链接必须用于访问websocket吗?只用wss而不是https?
在此之后,我尝试将客户端连接到服务器并在onMessage方法中接收消息.
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container.connectToServer(ClientEndpoint.class, buildCEC(), new URI("wss://async.example.com/socket.io/1/?t=" + System.currentTimeMillis()));
我尝试在buildCec()中插入标题以连接到服务器.
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
Configurator conf = config.getConfigurator();
`Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Connection", Arrays.asList("keep-alive", "Upgrade"));
headers.put("Sec-Websocket-Extensions", Arrays.asList("x-webkit-deflate-frame"));
headers.put("Sec-Websocket-key", Arrays.asList("cx36sHrtuW9HZAaB6kKa/Q=="));
headers.put("Sec-Websocket-Version", Arrays.asList("13"));
headers.put("Upgrade", Arrays.asList("websocket"));
headers.put("User-Agent", Arrays.asList("Mozill..."));
conf.beforeRequest(headers);
return config;`
Run Code Online (Sandbox Code Playgroud)问题
我正在使用一个程序设计端点并导入javax.websocket.ContainerProvider; 用于处理来自jsr356的websocket连接.
Joa*_*elt 12
这里有很多主题.
Sec-WebSocket-Key在RFC6455多次提及,它有1个总体目标,为客户证明它接收到一个有效的WebSocket开放握手从服务器.客户端有规则,服务器端有关它应该做什么的规则,以及它应该如何响应它.(简而言之,它是客户端选择的随机值,发送到服务器,服务器在RFC指定的GUID上执行此步骤,计算SHA-1哈希值,并使用此哈希值进行响应,Sec-WebSocket-Key客户端验证为确认服务器真的支持WebSocket)javax.websocket.server.ServerEndpointConfig.Configurator那些方法不能被你调用.这些是在建立连接的过程中由JSR-356 websocket服务器实现调用的.请参阅我关于此过程的其他答案 - 在Web Socket @ServerEndpoint中从HttpServletRequest访问HttpSession我知道有几个JSR-356独立客户端.
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-client-impl</artifactId>
<version>9.1.0.v20131115</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-websocket</artifactId>
<version>8.0.0-RC5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-coyote</artifactId>
<version>8.0.0-RC5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-websockets</artifactId>
<version>2.3.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>1.3.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果您确实编写了自己的WebSocket客户端,请确保您针对Autobahn WebSocket Protocol Testsuite进行测试(每个人都针对此测试套件编写实现测试,它有点成为事实上的协议验证套件)
| 归档时间: |
|
| 查看次数: |
9211 次 |
| 最近记录: |