ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Communication / WebSocket
  • Overview
  • How the WebSocket server works
  • How to use the WebSocket server?
  • "WebSocket server" project
  • Creating a "WebSocket server" project
  • WLanguage events specific to "WebSocket server" projects
  • Execution contexts
  • Specific WLanguage functions that can be used in WebSocket server projects
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
The WebSocket protocol provides a communication channel over a TCP socket for browsers and Web servers. Thus, it is possible to communicate and exchange messages with a Web server, using WEBDEV browser code.
The WebSocket server provided with WEBDEV can be run on Windows and with IIS (IIS 8 or later) only.
Starting with version 27, it is possible to connect to a WebSocket server via a WINDEV application (Windows or Linux) or a WINDEV Mobile application (Android or iOS).
How the WebSocket server works
In an application or Web site, the client sends a request to the server and the server returns the response, such as the content of the requested page.
The use of a WebSocket makes it possible to transmit data associated with the website in real time (e.g., messages in an instant messaging site). With the WebSocket protocol, the client only needs to establish a connection with a Web server. Once established, the communication channel remains open.
The server can independently send all the information it considers useful to the client. If new information is available on the server, the client does not have to send a request.
How to use the WebSocket server?
To use the WebSocket server, you must:
  1. Install the WEBDEV 2024 Application Server, which allows you to manage WebSocket servers ("WebSockets" tab).
    Remark: WEBDEV 26 Application Server or later is required.
  2. Create a "WebSocket server" project. This project is deployed on the Web server (where the WEBDEV Application Server has been installed). It waits for connections sent to it using the WebSocket protocol. This application runs in the background on the server.
  3. Create the project that will query the WebSocket server. This project will send messages to the WebSocket server and process the responses using Socketxxx functions, if necessary. This project can be:
    • A WEBDEV project.
    • A WINDEV project.
    • A WINDEV Mobile project (Android or iOS).
  4. The connection to the WebSocket server can be established from the "client" project via the following functions:
    WebSocketClientConnectEstablishes a connection between the client and a WebSocket server.
    WebSocketClientConnectSSLEstablishes an SSL connection between the client and a WebSocket server.
Remark: The WEBDEV WebSocket Server can be used with any language that supports this protocol.
"WebSocket server" project

Creating a "WebSocket server" project

To create a "WebSocket server" project:
  1. Go to the WEBDEV home page (or press CTRL + <).
  2. Click "Create a project", then "WebSocket server".
  3. The project creation wizard starts.
  4. Specify the name of the project and follow the different steps of the project creation (for more details, see Creating a WEBDEV project).
  5. Finish the wizard: the project is automatically created.
Caution: This project must not use (internal or external) components, or load libraries (WDL).

WLanguage events specific to "WebSocket server" projects

Several specific WLanguage events are associated with the project:
EventRuntime condition
Global service (loop process)This event is the main event of the WebSocket server. It is called in a loop when the WebSocket server is started.
For example, this event can be used to send a message to one or more clients according to a parameter read in a data file.
You can specify the execution interval of this event using the following syntax:
PROCEDURE GlobalService()<interval=<Duration>>
where <Duration> is the event execution interval. This parameter can be an integer that corresponds to the number of milliseconds, or the duration in text format (e.g., '20 s' or '10 ms'). By default, this event is executed every 10 seconds.
In this event, you can use WebSocketListConnectedClient to get the list of clients connected at the time of execution of the function (keep in mind that this list can be different at each moment).
New connectionExecuted when a client connection is requested (via SocketConnect).
Reminder: The client is a website. The connection request will be made from the browser code of a web page.
This event accepts parameters. This event has the following format:
PROCEDURE Connection(Client is websocketClient)
where <Client> corresponds to a websocketClient variable that contains the characteristics of the client.
Receive a message from a clientExecuted when the client sends a message to the WebSocket server (via SocketWrite).
This event accepts parameters. This event has the following format:
PROCEDURE ReceiveMessage(Client is websocketClient,
vMessage is Variant)
where:
  • <Client> corresponds to a websocketClient variable that contains the characteristics of the client.
  • <vMessage> corresponds to the message sent by the client. The actual type of the stored value is a Unicode string or a Buffer (<vMessage>.Type).
Disconnection of a clientExecuted when the client is disconnected (via SocketClose).
This event accepts parameters. This event has the following format:
PROCEDURE Disconnection(Client is websocketClient)
where <Client> corresponds to a websocketClient variable that contains the characteristics of the client.
Stop global serviceExecuted when the global service is stopped.

Remark: To edit the specific events of a project: on the "Home" tab, in the "General" group, expand and select "Project code".

Execution contexts

The global service of the WebSocket server and the connected clients are executed independently. Each is executed in its own context with:
  • its own copy of global variables.
  • its own database context.
  • etc.
Therefore, it is not possible to handle the global context or the context of another client from another context.
WebSocketExecute is used to execute a procedure in the context of a client connected to the WebSocket server. In this case, the execution context is that of the client: during the execution of the procedure, the global variables will be those of the specified client.
WebSocketExecuteGlobalService is used to execute a global procedure in the global context of the WebSocket server. During the execution of the global procedure, the global variables will be those of the global service.
Caution: It may be necessary to set the time interval between two global service calls so that the procedure can be executed.

Specific WLanguage functions that can be used in WebSocket server projects

The following functions can be used in the WebSocket server:
WebSocketDisconnectDisconnects a client from a WebSocket server.
WebSocketExecuteExecutes a procedure in the context of a client connected to the WebSocket server.
WebSocketExecuteGlobalServiceExecutes a global procedure in the context of the global service of the WebSocket server.
WebSocketListConnectedUserLists the clients connected to the WebSocket server.
WebSocketSendSends a message from the WebSocket server to the client.
Minimum version required
  • Version 26
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/04/2023

Send a report | Local help