Scriptico

WebORB Data Push. How to push data to a particular client

The following article shows how to push data from the WebORB server to a connected client

Obviously, if you want to push data to a particular client you must identify the client somehow. Therefore, the first issue is a client identification and it might be done in two different ways.

The first way to establish the client on the client side. In this case, the identification process as described below:

  • 1. Client has some ID before establishing a connection to the server
  • 2. Client passes the ID to the server as a connection parameter
  • 3. Server creates a relationship between the client connection and the received client id

The following example code demonstrates the use-case above:

Client

private var _connection:NetConnection;
			private var _clientId:String;
			
			private function init():void
			{
				_clientId = UIDUtil.createUID();
				
				_connection = new NetConnection();
				_connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusEvent);
				
				_connection.connect("rtmp://localhost:2037/dpso", _clientId);
			}

Server

public override bool appConnect(Weborb.Messaging.Api.IConnection conn, object[] parms)
        {
            bool connected = base.appConnect(conn, parms);
            String clientId = "";
            if(parms.Length > 0)
                clientId = parms[0] as String;

            if (connected)
                conn.setAttribute("clientId", clientId);

            return connected;
        }

The second way is the client identification by the server side. Client connects to the server, the server allocate a new client id and creates a relationship between the client connection and the allocated id. A simple implementation of this approach is shown below:

Client

private var _connection:NetConnection;
			private var _clientId:String;

private function init():void
			{
				_connection = new NetConnection();
				_connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusEvent);
				_connection.connect("rtmp://localhost:2037/dpso");
			}

private function onNetStatusEvent(event:NetStatusEvent):void
			{
				switch( event.info.code )
				{
					case "NetConnection.Connect.Success":  
						callLater(getAssignedId);
						break;
					
					default:
						break;
				}
			}

private function getAllocatedId():void
			{
				_connection.call("getAllocatedId", new Responder(
					function(result:String):void
					{
						_clientId = result;
					}));
			}

Server

 public override bool appConnect(Weborb.Messaging.Api.IConnection conn, object[] parms)
        {
            bool connected = base.appConnect(conn, parms);
            
            String clientId = Guid.NewGuid().ToString();

            conn.setAttribute("clientId", clientId);

            return connected;
        }

        public String getAllocatedId()
        {
            String clientId = ConnectionHub.getConnectionLocal().getAttribute("clientId") as String;
            return clientId;
        }

Now we can identificate all connected clients. Now that we have ids for all clients and the following code demonstrates how to push data:

Server

 private void pushData(String methodName, object[] args)
        {
            if (scope.getClients().Count == 0)
                return;

            IEnumerator connections = scope.getConnections();
            while (connections.MoveNext())
            {
                IConnection connection = connections.Current;
                if (connection is IServiceCapableConnection && connection.getAttribute("clientId").Equals("8dfc75bc-9568-43db-a5b1-17ad16b31b88"))
                {
                    ((IServiceCapableConnection)connection).invoke(methodName, args);
                }
            }
        }

That is it! Let me know if you have any questions!

Resources

Category: Examples, WebORB (.NET)

Tagged:

Leave a Reply

*