我的目标:获取并解析套接字的新数据的每一滴滴,我不会发送任何数据。
我正在使用 Socket.IO 库,并且试图首次连接到 websocket服务器,所以我不了解大多数含义变量,我想编写一个简单连接的代码(用于解析数据,不发送)。
这是我从 Chrome检查器(f12按钮)->网络-> WS->标头 抓取的标头,这些信息足以连接到套接字并读取数据:
常规
Request URL: wss://tradeit.gg/socket.io/?EIO=3&transport=websocket&sid=jDTknMoBYlV7VOR3AWOj
Request Method: GET
Status Code: 101 Switching Protocols
响应标头
CF-RAY: 44824ea40aaf8b58-KBP
Connection: upgrade
Date: Fri, 10 Aug 2018 12:01:20 GMT
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Sec-WebSocket-Accept: xToN6aWToIogTQo3cOFKZpn6a44=
Sec-WebSocket-Extensions: permessage-deflate; client_no_context_takeover
Sec-WebSocket-Version: 13
Server: cloudflare
Upgrade: websocket
WebSocket-Server: uWebSockets
请求标头
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: no-cache
Connection: Upgrade
Cookie: __cfduid=d4f3e1f3363b2ffcec6209cae50f95df91530538054; _ga=GA1.2.642241760.1530538057; _gid=GA1.2.1752480950.1533821233; smaller_icons=true; io=jDTknMoBYlV7VOR3AWOj
Host: tradeit.gg
Origin: https://tradeit.gg
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: pMGG6zJSm8CF97iHAvyWrw==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
查询字符串参数
EIO: 3
transport: websocket
sid: jDTknMoBYlV7VOR3AWOj
我确定我提供了很多额外的信息,但这是因为我不确定要连接的代码需要哪些信息:
Socket socket = IO.socket("");
socket.on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
public void call(Object... objects) {
Transport transport = null;
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
public void call(Object... objects) {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Cookie", Arrays.asList("tokenName1=tokenValue1",
"tokenNameN=tokenValueN"));
}
});
transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
public void call(Object... objects) {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
String cookie = headers.get("Set-Cookie").get(0);
System.out.println(cookie);
}
});
}
});
所以有四个问题:
1.我需要在IO.Socket支架中放入什么?
2.我需要运输什么?
3.我要放置哪些标头才能成功连接?
4. 如何获取每个websocket的数据?