Difference between revisions of "Websocket"
From Lianjapedia
Yvonne.milne (Talk | contribs) (Created page with "''Under Construction'' The Websocket class allows the application to handle asynchronous notifications from a remote server. See also: [https://www.lianja.com/community/show...") |
Yvonne.milne (Talk | contribs) (→Methods) |
||
(12 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | The Websocket class allows an application to handle asynchronous notifications from a remote server. | |
− | + | This class is available on the client in a browser and on the server. You typically run a background websocket task using lianjarun. Be sure to open up the port you are using on your server firewall. | |
− | + | Typical uses of websockets are to handle low latency messages between a client browser and the server where near real time response is required. | |
Note: property, method and event names should be referred to in lowercase in case-sensitive scripting languages. | Note: property, method and event names should be referred to in lowercase in case-sensitive scripting languages. | ||
Line 17: | Line 17: | ||
!width="50%"|Description | !width="50%"|Description | ||
|- | |- | ||
− | |valign="top"| | + | |valign="top"|onconnected |
|valign="top"|RW | |valign="top"|RW | ||
|valign="top"|Function | |valign="top"|Function | ||
− | |valign="top"|Function to call when | + | |valign="top"|Function to call when socket connected |
+ | |- | ||
+ | |valign="top"|ondisconnected | ||
+ | |valign="top"|RW | ||
+ | |valign="top"|Function | ||
+ | |valign="top"|Function to call when socket disconnected | ||
+ | |- | ||
+ | |valign="top"|onerror | ||
+ | |valign="top"|RW | ||
+ | |valign="top"|Function | ||
+ | |valign="top"|Function to call when error occurred | ||
|- | |- | ||
|valign="top"|receivedmessage | |valign="top"|receivedmessage | ||
|valign="top"|RW | |valign="top"|RW | ||
|valign="top"|Function | |valign="top"|Function | ||
− | |valign="top"|Function to call when message received | + | |valign="top"|Function to call when message received |
|- | |- | ||
|} | |} | ||
Line 47: | Line 57: | ||
|- | |- | ||
|valign="top"|listen | |valign="top"|listen | ||
− | |valign="top"|port as Numeric | + | |valign="top"|port as Numeric[,ssl as logical] |
− | |valign="top"|Listens on the specified port (server) | + | |valign="top"|Listens on the specified port (server). If ssl is true then use wss:/ to connect using secure connection on client else use ws:/ |
|- | |- | ||
|valign="top"|sendmessage | |valign="top"|sendmessage | ||
Line 55: | Line 65: | ||
|- | |- | ||
|valign="top"|sendmessage | |valign="top"|sendmessage | ||
− | |valign="top"|websocketid as , message as Character | + | |valign="top"|websocketid as Numeric, message as Character |
|valign="top"|Sends the specified message to the specified web socket id (from server) | |valign="top"|Sends the specified message to the specified web socket id (from server) | ||
+ | |- | ||
+ | |valign="top"|setlocalcertificate | ||
+ | |valign="top"|filename as Character<br>e.g. localhost.crt | ||
+ | |valign="top"|Specifies the path to the local certificate file for wss:/ SSL connection. From v6.3. | ||
+ | |- | ||
+ | |valign="top"|setpathtocertificate | ||
+ | |valign="top"|filename as Character<br>e.g. myserver.crt or myserver.pem | ||
+ | |valign="top"|Specifies the path to the certificate file for an SSL connection (server) | ||
+ | |- | ||
+ | |valign="top"|setpathtoprivatekey | ||
+ | |valign="top"|filename as Character<br>e.g myserver.key or myserver.pem | ||
+ | |valign="top"|Specifies the path to the private key file for an SSL connection (server) | ||
+ | |- | ||
+ | |valign="top"|setpathtocacertificatedir | ||
+ | |valign="top"|path as Character<br>e.g /etc/ssl/certs | ||
+ | |valign="top"|Specifies the path to the CA for an SSL connection (server) | ||
+ | |- | ||
+ | |valign="top"|setprivatekey | ||
+ | |valign="top"|filename as Character<br>e.g. localhost.key | ||
+ | |valign="top"|Specifies the path to the local key file for wss:/ SSL connection. From v6.3. | ||
|- | |- | ||
|} | |} | ||
+ | |||
+ | ====Example==== | ||
+ | Server side LianjaScript (.prg): | ||
+ | <code lang="recital"> | ||
+ | // This is the callback for all messages received on the websocket. | ||
+ | // | ||
+ | // Arguments: | ||
+ | // | ||
+ | // wsid is the WebSocketID, you must specify this when sending a message back to the client using sendMessage | ||
+ | // cMessage is the message. Use json_encode() and json_decode() to pass objects between the client and the server. | ||
+ | // | ||
+ | proc receivedMessage(wsid, cMessage) | ||
+ | if cMessage = "quit" | ||
+ | clear events | ||
+ | endif | ||
+ | lo.sendMessage(wsid, cMessage) | ||
+ | endproc | ||
+ | |||
+ | private lo = createObject("websocket") | ||
+ | lo.receivedMessage = receivedMessage | ||
+ | lo.listen(8004) | ||
+ | // or listen(8004, true) for secure SSL connection | ||
+ | wait events | ||
+ | </code> | ||
+ | |||
+ | Client side JavaScript function: | ||
+ | <code lang="javascript"> | ||
+ | //////////////////////////////////////////////////////////////// | ||
+ | // Event delegate for 'click' event | ||
+ | function page1_section1_field1_click() | ||
+ | { | ||
+ | var lo = Lianja.createObject("websocket"); | ||
+ | |||
+ | // declare callbacks | ||
+ | lo.receivedMessage = function(cMessage) | ||
+ | { | ||
+ | console.log("Received message: " + cMessage); | ||
+ | }; | ||
+ | |||
+ | lo.onconnected = function() | ||
+ | { | ||
+ | console.log("onconnected()"); | ||
+ | }; | ||
+ | |||
+ | lo.ondisconnected = function() | ||
+ | { | ||
+ | console.log("ondisconnected()"); | ||
+ | }; | ||
+ | |||
+ | lo.onerror = function() | ||
+ | { | ||
+ | console.log("onerror()"); | ||
+ | }; | ||
+ | |||
+ | // connect to the remote WebSocket Server | ||
+ | lo.connect("ws://localhost:8004"); | ||
+ | // or lo.connect("wss://localhost:8004"); for a secure connection | ||
+ | |||
+ | // messages are queued up until the websocket is connected. | ||
+ | lo.sendMessage("Hello world"); | ||
+ | lo.sendMessage("quit"); | ||
+ | |||
+ | // disconnect is deferred until all messages are sent and received | ||
+ | lo.disconnect(); | ||
+ | |||
+ | return true; | ||
+ | }; | ||
+ | </code> | ||
[[Category:Documentation]] | [[Category:Documentation]] |
Latest revision as of 08:05, 24 November 2021
The Websocket class allows an application to handle asynchronous notifications from a remote server.
This class is available on the client in a browser and on the server. You typically run a background websocket task using lianjarun. Be sure to open up the port you are using on your server firewall.
Typical uses of websockets are to handle low latency messages between a client browser and the server where near real time response is required.
Note: property, method and event names should be referred to in lowercase in case-sensitive scripting languages.
Properties
This class supports the following properties:
Property | Access (R/RW) | Value | Description |
---|---|---|---|
onconnected | RW | Function | Function to call when socket connected |
ondisconnected | RW | Function | Function to call when socket disconnected |
onerror | RW | Function | Function to call when error occurred |
receivedmessage | RW | Function | Function to call when message received |
Methods
This class supports the following methods:
Method | Args | Description |
---|---|---|
connect | url as Character | Connects to the server |
disconnect | None | Disconnects from the server |
listen | port as Numeric[,ssl as logical] | Listens on the specified port (server). If ssl is true then use wss:/ to connect using secure connection on client else use ws:/ |
sendmessage | message as Character | Sends the specified message to the server (from client) |
sendmessage | websocketid as Numeric, message as Character | Sends the specified message to the specified web socket id (from server) |
setlocalcertificate | filename as Character e.g. localhost.crt |
Specifies the path to the local certificate file for wss:/ SSL connection. From v6.3. |
setpathtocertificate | filename as Character e.g. myserver.crt or myserver.pem |
Specifies the path to the certificate file for an SSL connection (server) |
setpathtoprivatekey | filename as Character e.g myserver.key or myserver.pem |
Specifies the path to the private key file for an SSL connection (server) |
setpathtocacertificatedir | path as Character e.g /etc/ssl/certs |
Specifies the path to the CA for an SSL connection (server) |
setprivatekey | filename as Character e.g. localhost.key |
Specifies the path to the local key file for wss:/ SSL connection. From v6.3. |
Example
Server side LianjaScript (.prg):
// This is the callback for all messages received on the websocket. // // Arguments: // // wsid is the WebSocketID, you must specify this when sending a message back to the client using sendMessage // cMessage is the message. Use json_encode() and json_decode() to pass objects between the client and the server. // proc receivedMessage(wsid, cMessage) if cMessage = "quit" clear events endif lo.sendMessage(wsid, cMessage) endproc private lo = createObject("websocket") lo.receivedMessage = receivedMessage lo.listen(8004) // or listen(8004, true) for secure SSL connection wait events
Client side JavaScript function:
//////////////////////////////////////////////////////////////// // Event delegate for 'click' event function page1_section1_field1_click() { var lo = Lianja.createObject("websocket"); // declare callbacks lo.receivedMessage = function(cMessage) { console.log("Received message: " + cMessage); }; lo.onconnected = function() { console.log("onconnected()"); }; lo.ondisconnected = function() { console.log("ondisconnected()"); }; lo.onerror = function() { console.log("onerror()"); }; // connect to the remote WebSocket Server lo.connect("ws://localhost:8004"); // or lo.connect("wss://localhost:8004"); for a secure connection // messages are queued up until the websocket is connected. lo.sendMessage("Hello world"); lo.sendMessage("quit"); // disconnect is deferred until all messages are sent and received lo.disconnect(); return true; };