Search code examples
urlwebsocketbrowserserveresp32

Can I directly type websocket address to connect to a server directly in a web browser's URL?


I am a newbie to web programming using an embedded controller (ESP32), I wish to know whether I can directly type, e.g. "ws://192.168.1.2/socketserver" to connect to a websocket of my mini-server (ESP32) in a web browser's URL? (where 192.168.1.2 is the ip address of the mini-server)

I ask this question because I can susscessfully write to URL of a browser e.g. http://192.168.1.2/Input?motorspeed=123 to transmit command to a HTTP server acted by the microcontroller above, but never success when I use the websocket method.

In fact, I can successfully send the websocket commands or normal http server commands using a javascript, this was verified by many sample codes.

This may be a silly question, its nice for any advice.

(information just necessary, in case:) My script in the ESP32 (parts of the code using Arduino IDE):

#include <ESPAsynWebSrv.h>
#include <AsyncTCP.h>
.
.
.
AsyncWebServer server(80);
AsynWebSocket wsInput("/socketserver");

void onWsInputEvent( ....){ ....do somethings }

Setup(){
   Server.on("/", HTTP_GET, handleRoot);
   Server.onNotFound(handleNotFound);
   wsInput.onEvent(onWsInputEvent);
   Server.addHandle(&wsInput);
}

Solution

  • I think I understand what you want to find out.

    The short answer is no, you can't have a full WebSocket connection just from a browser address bar and send commands without continuously running some sort of client program i.e. JavaScript in a web page.

    The web browser address bar will only send a one-off http request and load on the screen whatever the server response was and that's it. After the response is received the connection is closed.

    In simple terms, a web browser "address bar" doesn't know how to handle websockets, you need a web page with javascript for that.

    WebSockets are a big topic and difficult to explain in few words, but here is some very very simplified info.

    WebSockets use http protocol. The difference is that when you send a standard http request (type a link in the browser, send a post/get from a web form, use Ajax etc) the server receives the request, does what it has to do, returns the response and closes the connection.

    With a websockets, the client sends an initial http request with an "upgrade" directive in the header, the server responds with a handshake, client responds back and the connection stays open. Until, either the client or server explicitly close the connection or for some reason the connection drops and they get physically disconnected. You can see that that requires a continuously running client program to maintain the connection and that's why the browser's address bar doesn't work.

    Technically, you can make a simple web page with websockets implemented inside (couple of text fields, a send button and a bit of javascript) that can communicate with your ESP32. You can save that HTML code inside the ESP program. Then when you hit the ESP's url from the browser (ordinary http request) the ESP can return that saved HTML page to the browser and then you can chit-chat over websockets.