websockets.exceptions.ConnectionClosedError:code=1006(连接异常关闭[内部]),无原因Python 3.7 websockets

2024-06-09 07:15:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我也有同样的问题, 1006 Connection closed abnormally error with python 3.7 websockets但是我的用例不同。所以请帮我解决这个问题

我正在运行python websocket服务器。这是我的密码

import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

start_server = websockets.serve(echo, "192.168.#.##", 5000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

我使用ESP32连接到我的服务器。连接正常,服务器回显从ESP32发送的消息约30秒,服务器立即停止。这种情况经常发生。这是我收到的错误websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

我的ESP32代码如下

#include <WiFi.h>
#include <WebSocketClient.h>
#include <ArduinoJson.h> 
#include <ESP32Servo.h> 
const char* ssid     = "####";
const char* password = "####";
 
char path[] = "/";
char host[] = "192.168.#.##";

int M1A = 16; //motor drive input pins
int M1B = 17;
int M2A = 18;
int M2B = 27;
 
WebSocketClient webSocketClient;
WiFiClient client;

void connnect(){
   if (client.connect(host, 5000)) {
    Serial.println("Connected");
  } else {
    Serial.println("Connection failed.");
  }
 
  webSocketClient.path = path;
  webSocketClient.host = host;
  if (webSocketClient.handshake(client)) {
    Serial.println("Handshake successful");
  } else {
    Serial.println("Handshake failed.");
  }
}
void setup() {
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  delay(5000);
 
 connnect();
 pinMode(M1A, OUTPUT); //configure as inputs
  pinMode(M1B, OUTPUT);
  pinMode(M2A, OUTPUT);
  pinMode(M2B, OUTPUT);
 
}
 
void loop() {
   String data;
  
    if (client.connected()) {
   
    webSocketClient.sendData("right");
    webSocketClient.getData(data);
    if (data!= NULL){
      if (data =="right"){
        Serial.println("right");
        analogWrite(M1A, 160);//pwm signal
        analogWrite(M1B, 0);
        analogWrite(M2A, 255);
        analogWrite(M2B, 255);
        delay(100);
      }else if (data =="left"){
        Serial.println("left");
        analogWrite(M1A, 255);//pwm signal
        analogWrite(M1B, 255);
        analogWrite(M2A, 160);
        analogWrite(M2B, 0);
        
      }else if (data =="forward"){
        Serial.println("forward");
        analogWrite(M1A, 160);//pwm signal
        analogWrite(M1B, 0);
        analogWrite(M2A, 160);
        analogWrite(M2B, 0);
        
      }else{
         Serial.println("stop");
         analogWrite(M1A, 255);//pwm signal
         analogWrite(M1B, 255);
         analogWrite(M2A, 255);
         analogWrite(M2B, 255);
      }
    }
 
  } else {
    Serial.println("Client disconnected.");
    connnect();
  }
 
  delay(3000);
 
}

提前谢谢


Tags: 服务器dataifwebsocketsserialelsewifiwebsocket