Scriptico

GPSTracker. Windows Service + WebORB for .NET + Flex

The following example shows how to communicate with WebORB from a windows service, and update data on clients using RemoteSharedObject.

I took an idea for this example from the midnight coders forum:

mrodsilva wrote:
Hi There,

I’m looking to use WebORB, but I have a doubt.

We have some devices (hardware with GPRS connection). Those devices send information through socket connection to a Windows Service application which updates a SQL database.

I’m wondering how would it be possible to synchronize those changes to our Flex Application. Is it possible? How so? What changes do we have to make on our Windows Service? Is it better to replace this Windows Service with something else?

Regards,

Marcelo

I thought this would make an interesting example and decided to implemented it, with a few minor tweaks:


1. Although Marcelo mentioned SQL database in his description, I took it out and the windows service doesn’t deal with the database at all since the example focuses on messaging rather than database data access.

2. I used three predefined coordinates to emulate data from “some devices”. When the service is starting it initializes coordinates, connects to the WebORB  messaging application (I called it GPSTracker), creates a timer, and every three second invokes a server method through WebORB. The method then updates a Remote Shared Object (RSO). Client application (or more than one instance of it) also connects to the messaging application, gets the shared object, and every time when the server updates the RSO all connected clients receive a synchronization event. To visualize the updated data on the client side I used the google maps API.

Windows Service

WebORB provides support for server-to-server integration through the RTMPClient class ( read more ), and I used it in the service as shown below:

namespace GPSTrackerService
{
public class GPSTrackerService : ServiceBase, IPendingServiceCallback
{
...
private RTMPClient _rtmpClient;
...
protected override void OnStart(string[] args)
{
connect();
base.OnStart(args);
}

private void _timerElapsed(object sender, ElapsedEventArgs e)
{
_rtmpClient.invoke("onCoordinateUpdate", GetCoordinate(), new InvocationCallback());
}

private void connect()
{
_rtmpClient = new RTMPClient();
_rtmpClient.connect("localhost", 2037, "GPSTracker", null, this);
}
}
}

You can find a link to the complete project at the bottom of this post. So you can download, build, and install it as a windows service. Please make sure that the weborb.config file is placed in the root directory of the compiled and installed service.

WebORB Server
On the server we should create a messaging application, and add few lines of the code. First of all, in the appStart method I retrieve (or create a new one if it doesn’t exist) an instance of the remote shared object:

public override bool appStart(Weborb.Messaging.Api.IScope app)
{
 ....
        sharedObject = this.GetSharedObject(app, "gpsTrackerSO");
 .....
}

Next, I added a method which will be invoked by the connected windows service every three seconds:

public void onCoordinateUpdate(String latitude, String longitude)
{
          sharedObject.setAttribute("coordinate", new String[2] { latitude, longitude });
}

Third. Well, There is no third, let’s keep it simple.

Client
All clients use rtmp connection and remote shared object to communicate with the WebORB server:

private var _connection:NetConnection;
private var _sharedObject:SharedObject;

protected function initConnection():void
{
        _connection = new NetConnection();
        _connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusEvent );
        _connection.client = this;
        _connection.connect( "rtmp://localhost:2037/GPSTracker" );

        _sharedObject = SharedObject.getRemote( "gpsTrackerSO", _connection.uri, true, false );
        _sharedObject.client = this;
        _sharedObject.addEventListener( SyncEvent.SYNC, onSyncEvent );
        _sharedObject.connect( _connection );
}

Connected clients will receive the SyncEvent event every time when the server updates the gpsTrackerSO remote shared object. As I mentioned above, in the example I use the google maps API to visualize updated coordinates

.....
... 
private function onSyncEvent(evt:SyncEvent):void {                          
        var receivedCoordinate:Array = evt.currentTarget.data.coordinate as Array; 				                        
         var markerA:Marker = new Marker(                         
                new LatLng(Number(receivedCoordinate[0]), Number(receivedCoordinate[1])), 	                                 
                        new MarkerOptions({                                          
                                strokeStyle: new StrokeStyle({color: 0x987654}),                                          
                                fillStyle: new FillStyle({color: 0x223344, alpha: 0.8}),                                          
                                radius: 12,                                           
                                hasShadow: true                                 
                 }));                 
        
        map.clearOverlays();                 
        map.addOverlay(markerA);
} 

As you can see in the code I create a new overlay and add it to the map every time when the shared object is changed.

That is it!

Video

Resources
0. Source Code
1. WebORB for .NET
2. WebORB for .NET User Guide

3. Accessing remote shared objects from Flex with WebORB .NET
4. ActionScript® 3.0 Reference for the Adobe® Flash® Platform :: flash.net.SharedObject

5. Google Maps API
6. How to Create Managed Windows Service

Category: WebORB (.NET)

Tagged:

2 Responses

  1. Marcelo says:

    Hi Cyril,

    I was trying your sample without success. The Windows Service GPSTrackerService, does’nt connect to the messaging application.

    The Flex Client connects normally.

    What am I doing wrong?

    Marcelo

  2. Cyril says:

    Hi Marcelo,

    please check the following:

    0. check if the messaging server is up. You can check it through the WebORB console. If the server is running, go to the next step.
    1. try to connect to the messaging application and then, check the WebORB log file. Do you see any exceptions? If so, try to figure out what causes it and if you can’t fix it or just need any help, share the log file with me. My email is cyril.deba@gmail.com
    2. If you have no exceptions on the server side, check the connection string in your windows service. Make sure that you are using the right host name, port, and the application name
    3. Try to debug the windows service and check if the server side returns any error messages

    Regards,
    Cyril

Leave a Reply

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.