Home > JavaScript, Object Pascal, OP4JS, Smart Mobile Studio > Messages in Smart Mobile Studio

Messages in Smart Mobile Studio

January 29, 2015 Leave a comment Go to comments

If you have missed message coding from Delphi under Smart Pascal then you are in for a treat. The QTX library now supports application-wide messages in the browser; allowing you to send and subscribe to as many messages as you like. More or less duplicating the stuff you are used to under WinAPI.

In fact, you can subscribe to the same message in the same object as many times as you want — and also instantiate as many subscription objects as you like.

Receiving messages

Here is how you would create and setup a message-subscription for a form:

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}

  //create our message subscriber object
  FMsgPort:=TQTXMessageSubscriber.Create;

  // subscribe to message-type $100
  FMsgPort.Subscribe($100,procedure (Message:TQTXMessageData)
    begin
      writeln("Data received:" + message.data);
    end);

  // subscribe to message-type $200
  FMsgPort.Subscribe($200,procedure (Message:TQTXMessageData)
    begin
      writeln("Data received:" + message.data);
    end);
end;

To send real objects through messages, simply use JSON to serialize it, then de-serialize it in your message handler.

Sending messages

The simplest way to send messages are through the two API methods:

  • QTX_SendMessage(Message:TQTXMessageData);
  • QTX_BroadcastMessage(Message:TQTXMessageData);

Alternatively, although highly unpractical, you could create your own message-port. The whole message system uses just a single message-port so you would never really create these yourself. If all you want to do is send and receive messages – you have more or less the same support as Delphi as for WinAPI messages — but simpler and easier to use!

Practical uses

Messages have been a part of Windows since the beginning, and it’s also hard-coded into Delphi. Their uses are almost limitless and you can use then to notify changes across controls and forms, transport data from one place to another — it’s really only creativity that sets the bounds.

What is cool about Smart Mobile Studio – is that you now have the same system under JavaScript. It works in every browser and allows your HTML5 forms and controls to communicate indirectly.

The technology is based on the HTML5 message system, which is a part of modern browsers. It’s extremely efficient and very reliable.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: