Websockets ESP8266

对着背影说爱祢 提交于 2021-01-28 04:52:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!