PowerWebSockets

PowerWebSockets Samples

READY-TO-RUN PROJECTS WITH SAMPLE CODE

Hello World messaging

Let’s get started by creating a simple PWS messaging service that will be receiving inbound requests and sending responses back to the client endpoint.

The first step is to include the Noemax.WebSockets.Messaging namespace, which provides the classes and extension methods that perform the PWS messaging.

Then we will create a class that represents our messaging service and annotate the methods that that will be handling inbound requests as remote operations.


			
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
using System;
using Noemax.WebSockets;
using Noemax.WebSockets.Messaging;
. . .
 
[MessagingContract]
class HelloWorldService : WebSocketMessagingService
{
   [MessageOperation("Hello")]
   private string Hello(string message)
   {
      return message;
   }
 
   [MessageOperation("GoodBye")]
   private string GoodBye()
   {
      return "GoodBye!";
   }
}

Creating the PWS service by inheriting the WebSocketMessagingService class is not a requirement, but it simply makes creating the service endpoint code shorter.

The service provides two messaging operations, Hello and GoodBye. These two operations are annotated with the MessageOperation attribute which also defines the action name associated with the operation. The action name will be used by remote endpoints when calling the operation. It doesn’t have to be the same as the name of the method.

In the application's Main entry point we create a WebSocketServer<HelloWorldService> instance for hosting our service and add a service endpoint on the URI "ws://localhost:8080".


			
01
02
03
04
05
06
07
08
09
10
11
. . .
static void Main(string[] args)
{
   var server = new WebSocketServer<HelloWorldService>();
   server.AddEndpoint("ws://localhost:8080");
   server.Open();
 
   Console.WriteLine("Server started. Press ENTER to exit..");
   Console.ReadLine();
   server.Close();
}

The WebSocketServer<ServerService> instance can listen on a single or on multiple endpoints. When it is ready to accept client connections, it automatically instantiates an instance of the service.

At this point the server side code is ready. Our next step is to create a client application that will be calling our service.


			
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
using System;
using Noemax.WebSockets;
using Noemax.WebSockets.Messaging;
. . .
class Program
{
   static void Main(string[] args)
   {
      var client = new WebSocketClient<WebSocketMessagingService>("ws://localhost:8080");
      client.Open();
 
      Console.WriteLine(client.Request<string>("Hello", "Hello there !!!"));
      Console.WriteLine(client.Request<string>("GoodBye"));
      Console.Read();
   }
}

We initialize a new client with the WebSocketMessagingService because it also provides the client-side dispatching mechanism for sending requests that invoke remote service operations. This functionality is required by the Request<T>() method which is a method extension that is defined in the Noemax.WebSockets.Messaging namespace.

In the above code we use the Request<string> method to send requests to a remote service operation and then wait for a return value of String type to be received as response.

client.Request<string>("Hello", "Hello there !!!"); sends a request to a remote service operation with action name "Hello" and a single string parameter “Hello there!!!”, then waits for a response message with a string value as result.

client.Request<string>("GoodBye"); sends a request to a remote operation with action name "GoodBye" without any parameters, then waits for a response message with a string value as result.

The Noemax.WebSockets.Messaging namespace provides many more extension methods for exchanging messages that are not shown in this example. You can find out more about them in the Noemax.WebSockets.Messaging namespace in the Class Reference.

You can download the sample code from:
http://downloads.noemax.com/samples/powerwebsockets/HelloWorldJSON.zip