Go to main content

sncodews

Websockets

SNCODEWS

NAME

sncodews - Implement websockets in Extenso

SYNOPSIS

sncodews[OPTIONS]

DESCRIPTION

From: https://www.raywenderlich.com/13209594-an-introduction-to-websockets

A WebSocket is a network protocol that allows two-way communication between a server and client. Unlike HTTP, which uses a request and response pattern, WebSocket peers can send messages in either direction at any point in time.

WebSockets are often used for chat-based apps and other apps that need to continuously talk between server and client.

sncodews is the program within Extenso to implement the server side.

The client side is implement using javascript. Reference documentation can be found at https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

Websockets enable a faster communication (low latency) between the browser (client) and the server.

Also the server can send a message to the client.

Connection is established at the beginning between the client and the server and they stay connected.

Some example usage:

  • Social feeds
  • Multiplayer games
  • Collaborative editing/coding
  • Financial tickers
  • Sports updates
  • Multimedia chat
  • Location-based apps
  • Online education

The program sncodews is used to call a program in Sncode that will implement the communication protocol between the client  (usually in Javacript) and the server.

When a new connection is setup in sncodews, a uuid is assign to this connection. This uuid is valid until the user close the connection.

The script executed by sncodews receive the following variables:

  • sncodews_reason : reason of the call which can be:
    • %define SNCODEWS_NEW_CONNECTION 0;
    • %define SNCODEWS_CLOSE_CONNECTION 4;
    • %define SNCODEWS_RECEIVE_DATA 6;
    • %define SNCODEWS_SIGUSR1 -10;
    • %define SNCODEWS_SIGUSR2 -12;
    • %define SNCODEWS_NEW_SERVER -1;
  • sncodews_uuid uuid of the connection (valid when the connection is establish or when receiving data)
  • sncodews_data : string of the data. If this string is a JSON, it must be unstringify with unstringify(data);

sncodews support multiple functions:

EXAMPLE SERVER SIDE

%define SNCODEWS_NEW_CONNECTION 0;
%define SNCODEWS_CLOSE_CONNECTION 4;
%define SNCODEWS_RECEIVE_DATA 6;
%define SNCODEWS_SIGUSR1 -10;
%define SNCODEWS_SIGUSR2 -12;
%define SNCODEWS_NEW_SERVER -1;
%define SNCODEWS_SIGTERM -15;

function execute_protocol(reason, uuid, data)

    switch reason do
    
        case SNCODEWS_NEW_SERVER:
            ws::new_server(uuid);
        endc

        case SNCODEWS_NEW_CONNECTION:
            ws::new_connection(uuid);
        endc
    
        case SNCODEWS_CLOSE_CONNECTION:
            ws::close_connection(uuid);
        endc
    
        case SNCODEWS_RECEIVE_DATA:
            receive_data(uuid, data);
        endc
    
        case SNCODEWS_SIGUSR1:
            "Reception de signal\n"; flush();
            ws::signal();
        endc
    
        case SNCODEWS_SIGTERM:
            ws::close_server();
        endc
    
        default:
            "unknown reason: "; reason; "\n"; flush();
        endc
    ends
endf
execute_protocol(sncodews_reason, sncodews_uuid, sncodews_data);

OPTIONS

--source or -s site
Sets the site name : staging or html
--help or -h
help
--mime or -m mimefile
Specify mime type file
--exec or -e file
Execute a binary file
--conf or -c file
Specify the configuration file
--base or -b
set base document root based on the configuration file
--args or -u query_string
Specify a query string. This string can be parse using the cgidata function
--port or -p number
Specify port number for the socket

SEE ALSO

AUTHOR

Written by Pierre Laplante  <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.