问题
I'm trying to send data to a server with websocket in ESP8266, but the handshake don't work.
I'm sending the following sequence of AT commands:
AT+RST
AT+CWMODE=1
AT+CIPMODE=0
AT+CIPMUX=1
AT+CWJAP="ssid_my_network","password"
AT+CIPSTART=4,"TCP","ip_server",port
AT+CIPSEND=4,data_lenght
In this moment, i send the header:
GET ws:ip_server HTTP/1.1\r\n
Host: ip_server\r\n
Upgrade: websocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n
Sec-WebSocket-Version: 13\r\n
But, i don't receive the response from server. What am I doing wrong?
回答1:
HTTP headers must end with an empty line. You need to send another \r\n
.
GET ws:ip_server HTTP/1.1\r\n
Host: ip_server\r\n
Upgrade: websocket\r\n
Connection: Upgrade\r\n
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n
Sec-WebSocket-Version: 13\r\n
\r\n
回答2:
I think you can try to use WiFiClient instead of WebSocket (like this)
Some code from the above link to send a http GET request:
// Perform an HTTP GET request to a remote page
bool getPage() {
// Attempt to make a connection to the remote server
if ( !client.connect(http_site, http_port) ) {
return false;
}
// Make an HTTP GET request
client.println("GET /index.html HTTP/1.1");
client.print("Host: ");
client.println(http_site);
client.println("Connection: close");
client.println();
return true;
}
回答3:
Try removing ws uri after handshaking GET request. If you have no path, make it "/" . Also if your websocket server is not serving from 80, you'd need to denote it in header after host attribute.
We might say a possible version mismatch had occured between peers, but no answer. So we have a tiny hidden problem like proxy etc.
来源:https://stackoverflow.com/questions/34050377/websockets-esp8266