WebSocket (Socket function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
Line 30: Line 30:
When this negotiation completes successfully on a Janus Web Server thread, the connection immediately and irreversibly transitions to a Janus WebSocket connection.
When this negotiation completes successfully on a Janus Web Server thread, the connection immediately and irreversibly transitions to a Janus WebSocket connection.


From this point until the connection is closed, no Web method or $function calls are allowed. The server program must first call the WebSocket method to complete the transition to a Web Socket thread and thereafter may only call the SendWebSocket, ReceiveWebSocket, and Close methods.
From this point until the connection is closed, no Web method or $function calls are allowed. The server program must first call the WebSocket method to complete the transition to a Web Socket thread and thereafter may only call the Socket methods SendWebSocket, ReceiveWebSocket, and Close.


...
...

Revision as of 22:34, 15 August 2019

Convert Web thread to WebSocket thread (Socket class)

[Introduced in Model 204 7.8 βeta]


This page is under construction.

Syntax

%socket = [%(Socket):]WebSocket

Syntax terms

%outSocketSocket object
[%(Socket):] The optional class name in parentheses denotes a virtual constructor. See "Usage notes", below, for more information about invoking a virtual constructor.

Usage notes

The HTTP protocol has been indispensible in creating the Internet as it exists today. It's still the core protocol for Web browsers and many other applications, whether on PCs, servers, laptops, or mobile devices. It has been extended over the years to accommodate demands for streaming video and sound, transfer of data files, and many other uses.

Yet HTTP it still falls short in providing for efficient general purpose data transfer.

First, it is a half-duplex protocol, meaning that one side of the communication is always waiting for the other to reply. Second, the HTTP headers add considerable overhead to each interaction, wasting valuable bandwidth. Finally, each HTTP request requires its own TCP connection, which is generally closed after the server replies. This adds the overhead of connection establishment to every individual request. A typical web page requires several distinct http requests, each of which require separate TCP connections and its own set of http headers.

The Web Socket protocol was created to address these shortcomings. It is designed to allow full-duplex communication. Efficient framing of payload data uses minimal overhead. The protocol is end-user extensible. Most importantly, it can be deployed completely within an existing http/https framework. No firewall rule changes are required, and optional data masking avoids potential issues with caching proxies.

Web Sockets are supported in Janus Web Server as of Model 204 7.8.

The Janus Web implementation of Web Sockets requires some preparation before you can write your applications.

A Janus Web port is enabled for Web Socket communication with the WEBSOCKET parameter. Likewise, an individual Janus Web rule can either allow or disallow Web Socket connections with the WEBSOCKET and NOWEBSOCKET parameters, respectively.

An http request for a Web Socket connection looks very similar to a normal http GET request. A special "upgrade" header in a request differentiates a Web Socket upgrade from all others. The upgrade request along with other unique headers advertise a request to negotiate a switch from HTTP to the Web Socket protocol.

When this negotiation completes successfully on a Janus Web Server thread, the connection immediately and irreversibly transitions to a Janus WebSocket connection.

From this point until the connection is closed, no Web method or $function calls are allowed. The server program must first call the WebSocket method to complete the transition to a Web Socket thread and thereafter may only call the Socket methods SendWebSocket, ReceiveWebSocket, and Close.

...

This function will upgrade a Janus Web thread to use the WebSocket protocol for simple two-way data transfer between the http client and the server.

The Janus Web request must contain the required http headers to allow the upgrade to occur.

The Janus Web port must be enabled for Web Socket upgrades with the WEBSOCKET parameter. The WEBSOCKET option can be specified on either the port definition or the JANUS WEB rule for the URL.

Any other usage will result in request cancellation.

Examples

See also