Difference between revisions of "Working with Sockets and Web Services"
From Lianjapedia
Yvonne.milne (Talk | contribs) |
Yvonne.milne (Talk | contribs) |
||
Line 142: | Line 142: | ||
endif</pre> | endif</pre> | ||
− | [[Category: | + | [[Category:Functions]] |
+ | [[Category:Socket Functions]] | ||
+ | [[Category:Lianja VFP Extensions]] | ||
+ | [[Category:VFP Function Extensions]] |
Latest revision as of 11:04, 28 March 2023
Lianja App Builder has built-in support for TCPIP sockets. These provide the ability for you to produce and consume data to/from remote servers written in your favorite language e.g. Visual FoxPro, Python, PHP, and/or JavaScript (node.js).
Tip: Use these functions in timer delegates to handle user notifications and/or data broadcasting
Contents
SOCKET_SERVER()
socket_server(expN, expC)
Argument | Description |
---|---|
expN | The TCPIP port number to listen on |
expC | The procedure to call to handle the connection |
Example
proc myhandler(skt as numeric) private m_running = .t. do while m_running if socket_peek(skt) <= 0 sleep 1 loop endif data = socket_read(skt) // do what you want to do with the data enddo endproc // create a server listening on port 9000 skt = socket_server(9000, "myhandler") if skt < 0 // error endif
SOCKET_OPEN()
socket_open(expC, expN1 [,expn2])
Argument | Description |
---|---|
expC | The IP address or nodename |
expN1 | The TCPIP port number |
expN2 | The (optional) timeout in seconds |
Example
// connect to a remote server e.g. node.js listening on port 9000 skt = socket_open("mydomain.com", 9000) if skt < 0 // error endif
SOCKET_PEEK()
socket_peek(expN)
Argument | Description |
---|---|
expN | The socket number returned from socket_open() or socket_server() |
Example
if socket_peek(skt) > 0 // we have some data to read... endif
SOCKET_READ()
socket_read(expN)
Argument | Description |
---|---|
expN | The socket number returned from socket_open() or socket_server() |
Example
// read the JSON encoded object from the socket data = socket_read(skt) // assume we are connected to node.js (as an example) // and decode the JSON string myobj = json_decode(data) // now myobj is a VFP object with all the properties // (and subobjects/arrays etc)
SOCKET_WRITE()
socket_write(expN, expC)
Argument | Description |
---|---|
expN | The socket number returned from socket_open() or socket_server() |
expC | The data we want to send to the remote server |
Example
// assume we are connected to node.js (as an example) then // send the data in JSON format data = json_encode(myobj) // write the JSON encoded object to the socket m_count = socket_write(skt, data) if m_count != len(data) // error sending data endif
SOCKET_CLOSE()
socket_close(expN)
Argument | Description |
---|---|
expN | The socket number returned from socket_open() or socket_server() |
Example
// close the socket connection m_skt = socket_close(skt) if m_skt != skt // error closing socket endif socket_lasterror() None // check the socket operation if socket_lasterror() != 0 // remote server connection failed, try reconnecting endif