Jump to content

Search the Community

Showing results for tags 'node'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Categories

There are no results to display.

Forums

  • Welcome to Smart Mobile Studio
    • News and Information
    • Pre-Sales Questions
    • Smart In the Media
    • Smart Contests
    • Meta
  • Smart Mobile Studio Discussion
    • General
    • IDE
    • RTL
    • Code
    • Client Server
    • Platform
    • Graphics
    • Deployment
    • Suggestion box
  • Smart Mobile Studio support
    • Support
    • Bug report
  • General Discussion
    • Pascal
    • Delphi
    • Javascript
    • HTML/HTML5
    • CSS
  • Resources
    • Website
    • Download Smart Mobile Studio

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Website URL


Location


Interests


Company name


Position

Found 3 results

  1. lynkfs

    node ground zero

    Node is still a new beast to tame, and at times it drives me bonkers. To get my head around node, I started with a couple of very simple node programs (js) and how these could be translated into object pascal. The exercise also meant to discover which of the RTL node units to use in which circumstances. The first one is a node program which accesses www.random.org (a site which generates random integers) and prints out the value to the console. in plain vanilla javascript : var https = require('https'); //The url is: 'https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' var options = { host: 'www.random.org', path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new' }; callback = function(response) { var str = ''; //another chunk of data has been received, so append it to `str` response.on('data', function (chunk) { str += chunk; }); //the whole response has been received, so we just print it out here response.on('end', function () { console.log(str); }); } https.request(options, callback).end(); http and https are modules built into node itself, so no need to npm-install these separately. The code above makes a https request to this particular url. In the callback it catches 2 events : on-'data' and on-'end'. On-'data' is called every time a chunk of data has been read. Not sure what the standard chunk-size is, but it will be more than the size of a single integer, so this event will only occur once. After that the on-'end' event fires. and executing "node tryout.js" does indeed produce a random integer Converting this line-by-line to SMS : start a new node project (tryout.sproj) and delete unit1. Replace the code in the root unit (tryout) by this : function RequireModule(id: string): Variant; external 'require'; var https := RequireModule('https'); var options: variant := new JObject; options.host := 'www.random.org'; options.path := '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'; var callback: variant := new JObject; callback := procedure(response: variant) begin var str: string := ''; response.on('data', procedure(chunk: variant) begin str+= chunk; end); response.on('end', lambda asm console.log(@str); end; end); end; https.request(options, callback).end(); and compile. The compiled tryout.js in the output directory reads as var https, options$1, callback; https = require("https"); options$1 = {}; options$1.host = "www.random.org"; options$1.path = "\/integers\/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new"; callback = {}; callback = function (response) { var str = ""; str = ""; response.on("data",function (chunk) { str+=String(chunk); }); response.on("end",function () { console.log(str); }); }; https.request(options$1,callback).end(); which is close enough and when executed produces the same results. btw: the compilation output is slightly different when having the 'use main body' in the 'compiler/code generation' project options selected or unselected. However results are the same for both. The first line function RequireModule(id: string): Variant; external 'require'; is copied from NodeJS.Core.pas so obviously that unit could be included in this particular use-case. Note 1 : http(s) offers a shortcut for GET calls (http.get...), so the SMS project above could be shortened to function RequireModule(id: string): Variant; external 'require'; //or alternatively : uses NodeJS.Core; var https := RequireModule('https'); var callback: variant := new JObject; callback := procedure(response: variant) begin var str: string := ''; response.on('data', procedure(chunk: variant) begin str+= chunk; end); response.on('end', lambda //procedure() //begin asm console.log(@str); end; end); end; https.get('https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new', callback).end(); Note 2: Instead of reading chunks of data and appending these into a string (see the 'data' event above), the response of the http request can also be sent to a stream. In that case the code is slightly modified and even more simplified : function RequireModule(id: string): Variant; external 'require'; var https := RequireModule('https'); var callback: variant := new JObject; callback := procedure(response: variant) begin response.setEncoding('utf8'); response.on('readable', lambda asm console.log((@response).read()); end; end); end; https.get('https://forums.smartmobilestudio.com/topic/4652-node-ground-zero/', callback).end(); Note 3 : to make it all a bit more SMS-like : start a standard new Node project and replace the code in Unit1 by : unit Unit1; interface uses nodeBasics; type TNodeProgram = class(TObject) public procedure Execute; constructor Create; virtual; https: variant; procedure callback(response: variant); end; implementation constructor TNodeProgram.Create; begin inherited Create; https := RequireModule('https'); end; procedure TNodeProgram.callback(response: variant); begin response.setEncoding('utf8'); response.on('readable', lambda console.log(response.read()); end); end; procedure TNodeProgram.Execute; begin https.get('https://www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new', @callback).end(); end; end. and for now the bare-bones nodeBasics : unit nodeBasics; interface type JConsole = class external 'Console' public procedure log(data: Variant); end; var Console external 'console': JConsole; function RequireModule(id: string): Variant; external 'require'; implementation end.
  2. lynkfs

    Node startup

    I suppose it is not possible to start up node / a node server, from the client. or is it ?
  3. lynkfs

    node, mysql and socket.io

    It has been announced for some time that the new and upcoming SMS release will have added support for databases (via node) and client/server connections (sockets). This update has not happened yet so below is a snippet which may be of interest in the meantime. Starting point is an example in pure js (server=node, client = html/js) which defines an application which features a central MySql database. Every connected client gets the entries in the main table of the database listed and can add new entries. New entries are pushed to all connected users in real time and the app lists the number of all connected users as well. This example works straight out of the box. To rewrite the client js code in Smart pascal a wrapper is necessary around the socket.io library For this I used Christians ts2pas converter The translation is not perfect but since only 2 functions are used here ('emit' and 'on'), the only parts necessary are type JEmitter = class external function on(event: String; fn: procedure(data:variant)): JEmitter; end; JSocket = class external(JEmitter) function emit(event: String; args: array of Variant): JSocket; end; It works for me to re-build this into a proper component (TSocket) as well client code here server code here This all works quite well. However some problems are : Clientside all modern browsers work except Edge Serverside stability depends on the server-environment. Hosting node on localhost Win10 is not 100% stable (server scripts crashing, ports reportedly in use after recovery, node.js service needs to be stopped manually). Things like that Allegedly professional node service providers like Heroku are more stable .
×