0

I have a simple project with ESP8266 and spring boot server. And I want to send data from ESP to the server via websocket.

On ESP I`m using this library to create Stomp client: https://github.com/ukmaker/StompClient

Spring Boot ws config:

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.setApplicationDestinationPrefixes("/esp");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
    }

WS controller:

    @MessageMapping("/sensors")
    @CrossOrigin("*")
    public WsModel resendData(WsModel wsModel) {
        return wsModel;
    }

ESP8266:

#include "ESP8266WiFi.h"
#include <WebSocketsClient.h>
#include "StompClient.h"

const char* wlan_ssid = "******";
const char* wlan_password = "******";

const char* ws_host = "localhost";
const int ws_port = 9091;
const char* ws_baseurl = "/gs-guide-websocket"; 

WebSocketsClient  webSocket;

Stomp::StompClient stomper(webSocket, ws_host, ws_port, ws_baseurl, true);

void setup(void)
{ 
  Serial.begin(115200);
  WiFi.begin(wlan_ssid, wlan_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  ...some code

  stomper.begin();
}

void loop() {
  webSocket.loop();
  stomper.sendMessage("/esp/sensors", "{\"name\":\"test\"}");
}

I don't have any error messages. It just doesn't send any data.

mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

0

Looking to the StompClient library examples SpringStompButtons.ino, we can see the comment :

const char* ws_baseurl = "/esp-websocket/"; // don't forget leading and trailing "/" !!!

In the file StompClient.h in the sockJs implementation there is :

  if (_sockjs) {
    socketUrl += random(0, 999);
    socketUrl += "/";
    socketUrl += random(0, 999999); // should be a random string, but this works (see )
    socketUrl += "/websocket";
  }

Then you should modify the socket url to :

const char* ws_baseurl = "/gs-guide-websocket/"; 

or disable sockJS creating stomp client with :

Stomp::StompClient stomper(webSocket, ws_host, ws_port, ws_baseurl, false);
mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • In addition to the answer, I created a tunnel to localhost using [ngrok](https://ngrok.com/) set a dynamically created link from ngrok to ws_host field and set field ws_port 80. – Dmitriy Ropay Jun 10 '19 at 15:19