How I developed my mobile services with Streamspin
|
Streamspin provides a middle-layer service which allows information to be pushed
to mobile phone users. When a third-party company uses Streamspin to deliver business
information to its mobile clients, the company only needs to connect its own business
logic and content to the Streamspin server, which will take care of all the other
steps to deliver the information to mobile users.
|
In order to develop my own services with Streamspin, I need to register an account at
www.streamspin.com.
|
In this tutorial, we have chosen to use .Net to program services, therefore I have Visual Studio or
Visual Studio Express installed at my pc. However, you can create services in a similar way using other
available APIs (see Streamspin APIs section).
|
To develop advanced applications, I need to have IIS or a web server running at
my pc.
|
In the following, I will describe how I created two mobile services “Hello, universe”
and “Tell my location” with Streamspin. The first service will be explained in detail
and the second one is created based on the steps finished in the first service.
|
|
|
I started with a simple service application which sends a "Hello, universe" message
to all the mobile clients that have subscribed to this service. I created an account
called “example1” at www.streamspin.com.
In order to make my service, I only need to
finish three steps.
- Registering and subscribing to the service
- Developing the service
- Testing the service
|
1. Registering and subscribing to the service
|
First, I registered the service and subscribed to this service.
|
As illustrated in Figure 1, after I logon to the system, I clicked the link “My
services” and chose the option “Create new” to create a new service called “Hello,
Universe.”
|
|
|
Figure 1. Creating the Service
|
|
After clicking the “Create Service” button, the new service will appear in the list
of my services (as in Figure 2).
|
|
|
Figure 2. View My Services
|
|
After I created the service, I need to subscribe to this service so that I can have
the service delivered to my mobile client. To subscribe to the service, I need to
find the new service (as illustrated in Figure 3) by clicking the “Services” option
|
|
|
Figure 3. Subscribe to the Service
|
|
on the webpage. Then, I need to click the “Subscribe” link in the “Hello, Universe”
service. Next, the “Hello, Universe” service will appear in the “Non-approved” section
in my subscriptions (as in Figure 4). The last step is to click the “Approve” link
of this service.
|
|
|
Figure 4. Approve the Service
|
|
Now, I have registered the service and subscribed to it. The next step is to develop
the concrete application of this service that sends a tip to the subscriber (myself)
through Streamspin.
|
2. Developing the service
|
As illustrated in Figure 5, I created a “Console Application” with Visual Studio
2005 and named it “HelloUniverse.”
|
|
|
Figure 5. Create a “HelloUniverse” Application
|
|
Then, as illustrated in Figure 6, I need to add the following two constants in the
program.
|
private const int serviceID = THE ID OF YOUR SERVICE
private const string key = YOUR SYMMETRIC KEY
|
|
|
Figure 6. Add serviceID and key
|
|
The value of “serviceID” can be found in the MyServices page (as illustrated in
Figure 7).
|
|
|
Figure 7. Find serviceID
|
|
The value of the symmetric key can be found in the MyInfo page (as illustrated in
Figure 8).
|
|
|
Figure 8. Find key value
|
|
After adding the two constants, I need to create a web-reference to the following
StreamSpin server services.
|
http://serverservices.streamspin.com/contentpublishing.asmx
|
In VisualStudio, I need to click the “Project “ menu and choose “Add Web Reference.”
As illustrated in Figure 9, in the next window, I input the link and add the reference
to the current project. I also named the web reference as “ServerServices”
|
|
|
Figure 9. Add web reference
|
|
The next step is to download the CryptoLib dll file which is used for creating timestamp
and signatures in the program. The download link can be found in the “Code Examples”
page when I logon to Streamspin. After I downloaded and unzipped the dll file, I
clicked the “Project” menu in Visual Studio and chose “Add reference.” As illustrated
in Figure 10, I chose the “Cryptolib.dll” file and imported it into the current
project. I also need to add using CryptoLib; to the program.
|
|
|
Figure 10. Add CryptoLib.dll
|
|
Now, I can paste the following code to the main method of the Program.cs file.
|
static void Main(string[] args)
{
ServerServices.ContentPublishing publisher = new ServerServices.ContentPublishing();
ServerServices.Content content = new ServerServices.Content();
content.Description = "My Streamspin demo";
content.Url = "Hello, Universe";
string timestamp = Signatures.CreateTimeStamp(DateTime.Now);
string toSign = content.Description + content.Url + serviceID + timestamp;
string signature = Signatures.Sign(toSign, key);
publisher.PublishPrivateContent(content, serviceID, signature, timestamp);
}
|
The idea of this program is to first create a content publisher object and a content
object. The content object is a wrapper for StreamSpin messages. Then we create
a timestamp which is required to avoid abuse by message playback. We create a signature
by signing the alphabetically sorted parameters, and finally we publish the content
to the StreamSpin server, which will pass the message on to any subscribed users.
Figure 11 illustrates the finished program of the console application.
|
|
|
Figure 11. The “Hello, Universe” Console Application
|
|
The coding step is finished. Now I can start to test my first mobile service.
|
|
|
A direct way of testing the service is to download the Streamspin client application
(the download link can be found at the “Downloads” page at Streamspin.com) and execute
the application at my mobile phone. The Streamspin project provides a very easy
way for developers to test their services without having a real phone. In the “CodeExamples”
page, I can download the “StreamSpin .Net Mobile Emulator User Control” and test
my service.
After I downloaded and unzipped the emulator, I created a new windows application
with Visual Studio and named it “StreamSpinEmulator” (as illustrated in Figure 12).
|
|
|
Figure 12. Create a Windows Application
|
|
The next step is to add the emulator to this project. I clicked the “Tools” menu
in Visual Studio and chose “Choose Toolbox Items.” Then I can choose to “Browse”
and add the “StreamSpinMobileEmulator.dll” to the current project (illustrated in
Figure 13).
|
|
|
Figure 13. Add the Emulator
|
|
Now, a new item called “MobileClient” appears in the “Toolbox” of Visual Studio
(as illustrated in Figure 14).
|
|
|
Figure 14. The Emulator Control
|
|
Next, I can drag this emulator control to the windows form and execute the program.
Since I have subscribed to this service, I can test it with my own account. As shown
in Figure 15, I need to first input my account information at Streamspin.com.
|
|
|
Figure 15. Login to the Emulator
|
|
After I logon to the emulator, I can execute the console application I just created
(as shown in Figure 16).
|
|
|
Figure 16. Execute the Console Application
|
|
After the console application is executed, we can go back to the running emulator
(Figure 17).
|
|
|
Figure 17. The Running Emulator
|
|
Now, a description “My Streamspin demo” appears in the top window of the emulator.
When I clicked this description on the top window of the emulator, the “Hello, Universe”
message shows up in the bottom window (as shown in Figure 18).
|
|
|
Figure 18. The Service Message
|
|
“Hello, Universe,” I finished my first mobile service with StreamSpin.
|
Part II. Tell My Locations
|
My next goal was to develop a service that can tell a mobile phone user where the
current location (the geographical latitude and longtitude) is. This service depends
on the assumption that either the user has a GPS integrated into the mobile phone
or the user explicitly points out to the StreamSpin server her/his current location.
In addition to the content-publishing web service (http://serverservices.streamspin.com/contentpublishing.asmx)
I just used in the first example, the Streamspin server provides another web service,
at http://serverservices.streamspin.com/UserLocation.asmx. This service retrieves
the current geographical location of the phone user.
In this “Tell My Locations” service, I need to create two parts to let subscribed
users of this service to receive their location coordinates whenever their current
locations are far away from the last reported locations. The idea is to create two
components, a web page that sends the location information to the phone users, and
a windows console application that register a location request to the StreamSpin
server, which will then send the location changes to the webpage. I will describe
the development in three steps.
- Generating the asp.net webpage
- Programming the Windows console application
- Testing the service
An important prerequisite of accomplishing this service is that I need to have an
online web server so that the asp.net webpage, when it is put on the server, can
be accessed by the Streamspin server. Fortunately, I have my web space at http://harry.streamspin.com.
Now I will proceed with the first development step.
|
1. Generating the asp.net webpage
|
To create the webpage, I opened Visual Studio, chose to create an “Asp.net website”
(as illustrated in Figure 19), and named it “TellMyLocation.”
|
|
|
Figure 19. Create the Website
|
|
In this service, we need to use another DLL file from the Streamspin project, which
encapsulates the usage of the web-services.
It is called “Streamspin .Net API.” It can be downloaded at the “Code Examples”
page at Streamspin.com. After I downloaded and unzipped the file, I clicked on the
“Website” menu of Visual Studio and chose “Add references” to add the DLL file to
the current project (as shown in Figure 20). Then, on top of all the programs in
“Default.aspx.cs” file, I need to add the following code.
using Com.Streamspin;
|
|
|
Figure 20. Add the Streamspin .Net Library
|
|
Now, having all the necessary library, I can start coding. The first thing to
do is to add the following two constants to the “Default.aspx.cs” file.
private const int serviceID = THE ID OF YOUR SERVICE
private const string myKey = YOUR SYMMETRIC KEY
This step is exactly the same as what I just did in the last example. I also re-use
the service ID (370) and the key of the account “example1”. Otherwise, I need to
redo the first step in Part I to create a new service and let myself subscribe to
this service. Figure 21 shows the code after I added the two constants.
|
|
|
Figure 21. Add Two Constants
|
|
Now, I need to paste the following code to the “Page_Load” function.
protected void Page_Load(object sender, EventArgs e)
{
Com.Streamspin.Key key = new Com.Streamspin.Key(myKey);
int userId = Convert.ToInt32(Request.QueryString["userId"]);
double lat = Convert.ToDouble(Request.QueryString["lat"]);
double lng = Convert.ToDouble(Request.QueryString["lng"]);
Com.Streamspin.Content content = new Com.Streamspin.Content();
content.Description = "My Streamspin location demo";
content.UrlOrBody = "<html><body>Your location<br> " +
"Latitude: " + lat.ToString() + "<br> " +
"Longitude: " + lng.ToString() + "<br> " +
"<img src='http://www.rico-wind.dk/daisy.gif'>";
Com.Streamspin.Publisher publisher = new Com.Streamspin.Publisher();
publisher.PublishContent(content, serviceID, key);
Response.Write("<returnvalue>true</returnvalue>");
Response.End();
}
The web code above will send a content message to the user every time the user’s
location changes. Figure 22 shows the whole program after I pasted the code.
|
|
|
Figure 22. Complete Code of the Webpage
|
|
The webpage step is done! I compiled the code and published the page at http://harry.streamspin.com/Default.aspx
(other published files accompanying this file in the virtual directory are also
uploaded).
|
2. Programming the Windows console application
|
The idea of having this console application is that this program will register a
location request to the Streamspin server. Following the similar steps in Section
of Part I, I opened another instance of Visual Studio, created a console application
and named it “RequestLocation” (as shown in Figure 23).
|
|
|
Figure 23. Create a Console Application
|
|
Then, similar to the last step, I need to add two constants to the “Program.cs”
file. Recall that I used the same serviceID (370) and key value.
private const int serviceID = THE ID OF YOUR SERVICE
private const string myKey = YOUR SYMMETRIC KEY
Like the webpage project, I also needed to add the Streamspin .Net API to this program
and add the following code on top of all the codes.
using Com.Streamspin
Now, I can add the following code to the Main method.
static void Main(string[] args)
{
Key key = new Key(myKey);
UserLocationRequest ul = new UserLocationRequest();
ul.AddRequest(9066, serviceID, 25, "http://harry.streamspin.com/Default.aspx",
key);
}
What happens here is that this program tells the StreamSpin location server to send
the location of the user. The changed location is send to the web-page I just created
above. The url is: http://harry.streamspin.com/Default.aspx. Note that in the “AddRequest”
method used in this program (the last lines), the first parameter is the userID,
which can be found in the “MyInfo” page at Streamspin.com (as shown in Figure 24).
|
|
|
Figure 24. Find userID
|
|
The third parameter is the distance threshold value which specifies how many meters
the user must move before she/he can be notified. Recall that the service I was
trying create here is to notify the mobile users whenever their current locations
are far way (i.e., to be over the distance threshold) from the last reported one.
Here I chose the value “25” (i.e., 25 meters) as the threshold. The fourth parameter
is where I put the webpage I just created. Figure 25 shows the whole program in
Visual studio.
|
|
|
Figure 25. Complete Code of the Console Application
|
|
Now, I completed all the coding part. It is time to test the service.
|
|
|
I used the account “example1” to subscribe to this service. Similar to the last
service, I planed to use the emulator to test the service. As it is impossible to
have GPS device, I need to manually create several locations for my account. This
is very easily done at the “My locations” page at Streamspin.com (as illustrated
in Figure 26).
|
|
|
Figure 26. Create Locations
|
|
Now I am ready to test the service. I reloaded the emulator project I used in the
previous service. As illustrated in Figure 27, after I logon, I can see that there
are three locations I can choose.
|
|
|
Figure 27. Three Available Locations
|
|
Now, I run the console application once (as shown in Figure 28).
|
|
|
Figure 28. Run the Console Application
|
|
Then, I can go back to the emulator screen. As shown in Figure 29, a new description
“Streamspin location demo” shows on the top window.
|
|
|
Figure 29. The Location Demo
|
|
When I clicked on the description, my current location coordinates are shown in
the lower window (as shown in Figure 30).
|
|
|
Figure 30. My Current Location
|
|
Now, I changed my location to “Location2,” which I just created on the Streamspin.com
(as shown in Figure 31).
|
|
|
Figure 31. Change Location
|
|
What shows up in the top window of the emulator is that the system senses this location
change and sends me another location coordinates (as shown in Figure 32).
|
|
|
Figure 32. New Location
|
|
Similarly, I did the same step to change to “Location3.” To let the Streamspin server
check my possible location changes, I need to re-execute the console application.
Then, as in Figure 33, I can have the new location data shown in the lower screen
of the emulator.
|
|
|
Figure 33. The “Location3” on the Screen
|
|
Instead of manually changing my location, I can also use a real mobile client with
GPS device. In that case I do not have to create locations and manually change them
to read my coordinates from this service.
Now, my second mobile service, “Tell My Location” is done!
|