Difference between revisions of "Websocket"

From Lianjapedia
Jump to: navigation, search
(Properties)
Line 1: Line 1:
''Under Construction''
 
 
 
The Websocket class allows the application to handle asynchronous notifications from a remote server.
 
The Websocket class allows the application to handle asynchronous notifications from a remote server.
 
See also: [https://www.lianja.com/community/showthread.php?5095-Working-with-web-sockets-in-Lianja-5-3 Working with web sockets in Lianja 5.3]
 
  
 
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 16: Line 12:
 
!Value
 
!Value
 
!width="50%"|Description
 
!width="50%"|Description
 +
|-
 +
|valign="top"|onconnected
 +
|valign="top"|RW
 +
|valign="top"|Function
 +
|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
Line 50: Line 61:
 
|-
 
|-
 
|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)
 
|-
 
|-
 
|}
 
|}
 +
 +
====Example====
 +
Server side Lianja/VFP Script (.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)
 +
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");
 +
 +
// 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]]

Revision as of 05:37, 6 May 2020

The Websocket class allows the application to handle asynchronous notifications from a remote server.

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 Listens on the specified port (server)
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)

Example

Server side Lianja/VFP Script (.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)
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");
 
	// 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;
};